Program Tip

멤버 이름은 둘러싸는 유형 C #과 같을 수 없습니다.

programtip 2020. 11. 7. 10:27
반응형

멤버 이름은 둘러싸는 유형 C #과 같을 수 없습니다.


아래 코드는 C #으로 작성되었으며 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");
        }
    }
}

위 코드를 컴파일 할 때이 오류가 발생합니다.

오류 : 'Flow': 멤버 이름은 엔 클로징 유형과 같을 수 없습니다.

왜? 어떻게 해결할 수 있습니까?


클래스 이름과 동일한 메서드 이름을 생성자 라고 합니다. 생성자에는 반환 유형이 없습니다. 따라서 다음과 같이 정확합니다.

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

또는 함수의 이름을 다음과 같이 변경하십시오.

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

전체 코드가 나에게 의미가 없지만.


문제는 방법에 있습니다.

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

클래스 이름이 지정 Flow되므로이 메서드의 이름도 지정할 수 없습니다 Flow. Flow이 코드를 컴파일 하려면 메서드 이름을 다른 이름으로 변경해야합니다 .

Or did you mean to create a private constructor to initialize your class? If that's the case, you will have to remove the void keyword to let the compiler know that your declaring a constructor.


Constructors don't return a type , just remove the return type which is void in your case. It would run fine then.


just remove this because constructor don't have a return type like void it will be like this :

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

As Constructor should be at the starting of the Class , you are facing the above issue . So, 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.


A constructor should no have a return type . remove void before each constructor .

Some very basic characteristic of a constructor :

a. Same name as class b. no return type. c. will be called every time an object is made with the class. for eg- in your program if u made two objects of Flow, Flow flow1=new Flow(); Flow flow2=new Flow(); then Flow constructor will be called for 2 times.

d. If you want to call the constructor just for once then declare that as static (static constructor) and dont forget to remove any access modifier from static constructor ..

참고URL : https://stackoverflow.com/questions/10070701/member-names-cannot-be-the-same-as-their-enclosing-type-c-sharp

반응형