2011-08-30 2 views
0

가능한 중복 :
How to initialize array of struct?
Initializing an Array of Structs in C#Initilize 구조체의 배열

의 C#, 비주얼 스튜디오 2010

내가 구조체의 배열을 선언하고 그것을 초기화합니다 같은 시간이지만 올바르게 이해할 수 없다. 구조체로 구성된 rray?

늘 컴파일러를 통해 이동하지만 전혀 가능 내가

private struct PrgStrTrans_t 
    { 
     public int id; 
     public string name; 
     public string fname; 
    } 

    private PrgStrTrans_t[] PrgStrTrans = { {1, "hello", "there"}, {2, "Fun", thisone"}} 

을 보관하고 싶은 생각을 보여 다음?

답변

1

구조체에 구조체를 추가하고 배열의 각 줄에 new PrgStrTrans(...),을 넣습니다. 이처럼

는 :

private struct PrgStrTrans_t 
{ 
    public int id; 
    public string name; 
    public string fname; 

    public PrgStrTrans_t(int i, string n, string f) 
    { 
     id = i; 
     name = n; 
     fname = f; 
    } 
} 

private PrgStrTrans_t[] PrgStrTrans = { 
              new PrgStrTrans_t(4, "test", "something"), 
              new PrgStrTrans_t(2, "abcd", "1234") 
             } 
+0

우수, 잘 작동합니다. –

0
private PrgStrTrans_t[] PrgStrTrans = { new PrgStrTrans_t() { id = 1, name = "hello", fname = "there"},new PrgStrTrans_t() {id = 2, name = "Fun", fname = "thisone"}}; 

당신이 생성자를 만든 경우가 더 나은 것, 그 속성 이름을 입력하지 않아도됩니다.

관련 문제