Member names cannot be the same as their closing type C #

The code below is in C # and I am using Visual Studio 2010.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace FrontEnd { class Flow { long i; private int x,y; public int X { get;set; } public int Y { get;set; } private void Flow() { X = x; Y = y; } public void NaturalNumbers(int x, int y) { for (i = 0; i < 9999; i++) { Console.WriteLine(i); } MessageBox.Show("done"); } } } 

When I compile the above code, I get this error:

Error: "Stream": member names cannot be the same as their closing type

Why? How can i solve this?

+68
c #
Apr 09 '12 at 8:25
source share
7 answers

Method names that match the name of the class are called constructors. Constructors do not have a return type. So right as:

 private Flow() { X = x; Y = y; } 

Or rename the function as:

 private void DoFlow() { X = x; Y = y; } 

Although all the code makes no sense to me.

+111
Apr 09 '12 at 8:28
source share

The problem is the method:

 private void Flow() { X = x; Y = y; } 

Your class is named Flow , so this method cannot also be called Flow . You will need to change the name of the Flow method to something else to compile this code.

Or did you want to create a private constructor to initialize your class? In this case, you will have to remove the void keyword so that the compiler knows that you are declaring the constructor.

+23
Apr 09 '12 at 8:29
source share

Constructors do not return a type, just remove the return type, which is not valid in your case. Then it will be ok.

+5
May 7 '15 at 18:53
source share

just delete this because the constructor does not have a return type such as void it will be as follows:

 private Flow() { X = x; Y = y; } 
+3
Nov 04 '14 at 20:43
source share

The constructor must not have a return type. remove void before each constructor.

Some very basic constructor characteristic:

but. Same name as class b. no return type. from. will be called every time an object is created with a class. for example, in your program, if you created two Flow objects, Flow flow1 = new Flow (); Flow flow2 = new Flow (); then the thread constructor will be called 2 times.

e. If you want to call the constructor only once, then declare it as static (static constructor) and do not forget to remove any access modifier from the static constructor ..

+2
Sep 20 '13 at 11:15
source share

Since the constructor should be at the beginning of the class, you are faced with the above problem. This way you can either change the name, or if you want to use it as a constructor, just copy the method at the beginning of the class.

+2
Jan 22 '17 at 11:13
source share

What is the use of X and Y in this class?

0
Jul 20 '19 at 10:16
source share



All Articles