2011-06-14 4 views

답변

11

몇 가지 옵션이 있습니다.

1) int의 기본값은 0이므로 초기화하지 않으면 0이됩니다.

2) 당신은 제로

private int _count = 0; 
public int Count { 
    get {return _count} 
    set {_count = value; } 
} 
15

int의 기본값은 null을하지 않을 수 0

모든 값 유형은 기본 값을 가질 수 있습니다.

이 MSDN 페이지의 Initializing Value Types을 참조하십시오.

+0

이 좋은 소식입니다 에프 아니면 나. 문자열과 bool 어때? 기본값은 ""이고 false입니까? – Geraldo

+0

@Geraldo - 아니요. 문자열은 참조 유형이므로 기본값은 'null'입니다. – Oded

+0

문자열을 기본값으로 설정할 수있는 방법이 있습니까? 아니면 각 문자열을 지정해야합니까? – Geraldo

7

Count은 값 유형이고 null이 아니므로 초기화시 기본값은 0입니다.

0

편집에 그 대신 자동 재산의 백업 필드를 사용하여 초기화 할 수 있습니다

public Topic(){ Count = 0;} 

3) 생성자를 사용할 수 있습니다

이 답변에 대한 의견을 통해 알 수 있듯이 이니셜 라이저 호출에 ()을 두는 것은 완전히 유효합니다.

var abc = new Topic() { 
    Topic = "test1", 
    Description = "description1" 
} 

합니다 (()주의) :

올바른 구문은 나의 선호하는 구문은 여전히 ​​ 것이다.

int의 기본값은 0이므로 Count이 0으로 초기화됩니다. 경우 당신은 항상, 주제 및 설명을 지정 명시 적으로 생성자를 추가 할 :

var abc = new Topic { 
    Topic = "test1", 
    Description = "description1" 
} 

그것은 생성자이고 객체 초기화 :

public Topic(string topic, string description) 
{ 
    Topic = topic; 
    Description = description; 
    // You may also set Count explicitly here, but if you want "0" you don't need to 
} 
+1

-1 :'()'에 대해주의 할 사항은 없습니다. – leppie

+0

응? OP는 그의 "생성자 호출"에 빈 인수 목록을 넣지 않았습니다. 나는 이것을 지적했고, 소스 코드를 굵게 만들 수 없기 때문에, 내가보기에 더 쉽게 볼 수 있도록 변경을 기록했습니다. 그게 나에게 -1을 주는가? –

+0

@Thorsten Dittmar : 구문에'()'이 있든 없든간에 아무런 차이가 없습니다. IL을 확인하십시오. – leppie

4

이 다음 관용구는 생성자뿐만 아니라.

실제로는 new Topic()이 먼저 호출되므로 모든 값이 기본값으로 초기화됩니다 (Topic 속성은 null이고 Description은 null이고 Count는 0 임). 그 후 "test1"값이 Topic에 할당되고 "description1"값이 Description에 할당됩니다.

모든 값 유형은 null이 아니므로 기본값과 다른 값을 가지며 참조 유형의 기본값은 null입니다.

0

공용 클래스 프로그램 { 공공 정적 무효 메인()는 {

// Declare a StudentName by using the constructor that has two parameters. 
    StudentName student1 = new StudentName("Craig", "Playstead"); 

    // Make the same declaration by using a collection initializer and sending 
    // arguments for the first and last names. The default constructor is 
    // invoked in processing this declaration, not the constructor that has 
    // two parameters. 
    StudentName student2 = new StudentName 
    { 
     FirstName = "Craig", 
     LastName = "Playstead", 
    }; 

    // Declare a StudentName by using a collection initializer and sending 
    // an argument for only the ID property. No corresponding constructor is 
    // necessary. Only the default constructor is used to process object 
    // initializers. 
    StudentName student3 = new StudentName 
    { 
     ID = 183 
    }; 

    // Declare a StudentName by using a collection initializer and sending 
    // arguments for all three properties. No corresponding constructor is 
    // defined in the class. 
    StudentName student4 = new StudentName 
    { 
     FirstName = "Craig", 
     LastName = "Playstead", 
     ID = 116 
    }; 

    System.Console.WriteLine(student1.ToString()); 
    System.Console.WriteLine(student2.ToString()); 
    System.Console.WriteLine(student3.ToString()); 
    System.Console.WriteLine(student4.ToString()); 
} 

// Output: 
// Craig 0 
// Craig 0 
// 183 
// Craig 116 

}

공용 클래스 StudentName {

// The default constructor has no parameters. The default constructor 
// is invoked in the processing of object initializers. 
// You can test this by changing the access modifier from public to 
// private. The declarations in Main that use object initializers will 
// fail. 
public StudentName() { } 

// The following constructor has parameters for two of the three 
// properties. 
public StudentName(string first, string last) 
{ 
    FirstName = first; 
    LastName = last; 
} 

// Properties. 
public string FirstName { get; set; } 
public string LastName { get; set; } 
public int ID { get; set; } 

public override string ToString() 
{ 
    return FirstName + " " + ID; 
} 

}

+0

KINDLY USE MSDN http://msdn.microsoft.com/en-us/library/bb397680 .aspx # Y255 – amod