2014-02-07 5 views
6

저는 C#을 처음 접했고 배우기도하고 단지 더미 테스트 프로그램입니다. 이 게시물의 제목에 언급 된 오류가 나타납니다. 아래는 C# 코드입니다.오류 : 멤버 이름은 그 동봉 형식과 같을 수 없습니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace DriveInfos 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program prog = new Program(); 
      prog.propertyInt = 5; 
      Console.WriteLine(prog.propertyInt); 
      Console.Read(); 
     } 

     class Program 
     { 
      public int propertyInt 
      { 
       get { return 1; } 
       set { Console.WriteLine(value); } 
      } 
     } 
    } 
} 
+5

오류 메시지의 어떤 부분이 불분명합니까? – hvd

+6

왜'Program'에 정의 된'Program'이 필요한가요? 그냥 이름 바꾸기! – crashmstr

+0

프로그램 내에서 사용되는 프로그램의 이름 변경 – Adrian

답변

7

당신은이 작업을 수행 할 때 :

Program prog = new Program(); 

당신이 원하는 경우 C# 컴파일러는 말할 수 없다 여기 Program을 사용하십시오.

namespace DriveInfos 
{ 
    class Program // This one? 
    { 
     static void Main(string[] args) 
     { 

또는 당신은 Program의 다른 정의를 사용하는 것을 의미하는 경우 :

class Program 
    { 
     public int propertyInt 
     { 
      get { return 1; } 
      set { Console.WriteLine(value); } 
     } 
    } 

여기에 가장 좋은 것은 당신에게 줄 것이다, 내부 클래스의 이름을 변경하는 것입니다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace DriveInfos 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MyProgramContext prog = new MyProgramContext(); 
      prog.propertyInt = 5; 
      Console.WriteLine(prog.propertyInt); 
      Console.Read(); 
     } 

     class MyProgramContext 
     { 
      public int propertyInt 
      { 
       get { return 1; } 
       set { Console.WriteLine(value); } 
      } 
     } 
    } 
} 

그래서 이제는 혼란이 없습니다. 컴파일러가 아니고 6 개월 후에 돌아와서 무엇을하고 있는지 알아 내려고 할 때도 아닙니다!

+2

컴파일러는 클래스가 실제로'Program'과'Program.Program'이므로이를 이해할 수있었습니다. 근본적인 문제는 C# 스펙이 ' 그러지 마. VB에서 이와 같은 이름의 중첩 클래스를 만들 수 있습니다. 나는 디자인 선택이 생성자 구문과 모호함과 혼동을 피하기 위해 만들어 졌다고 상상한다. – Chris

+1

@RobLang 나는 그것을 굵게 썼다. SOF의이 기능에 대해 알려 주셔서 감사합니다. 나는이 사실을 모르고 있었고, 나는 여기서도 새롭다. :-) –

2

당신은 같은 이름의 "프로그램"이름을 가진 두 개의 클래스가 그 중 하나

 
namespace DriveInfos 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program prog = new Program(); 
      prog.propertyInt = 5; 
      Console.WriteLine(prog.propertyInt); 
      Console.Read(); 
     } 

     class Program1 
     { 
      public int propertyInt 
      { 
       get { return 1; } 
       set { Console.WriteLine(value); } 
      } 
     } 
    } 
}