나는 다음과 같은 클래스가 생성자와 다소 혼동 스럽습니다. 이것이 가능합니까 아니면 abc를 만들 때 주제, 설명 및 개수를 지정해야합니까?C# 클래스 생성자 기본값 질문
답변
몇 가지 옵션이 있습니다.
1) int
의 기본값은 0이므로 초기화하지 않으면 0이됩니다.
2) 당신은 제로
private int _count = 0;
public int Count {
get {return _count}
set {_count = value; }
}
Count
은 값 유형이고 null
이 아니므로 초기화시 기본값은 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
}
이 다음 관용구는 생성자뿐만 아니라.
실제로는 new Topic()
이 먼저 호출되므로 모든 값이 기본값으로 초기화됩니다 (Topic 속성은 null이고 Description은 null이고 Count는 0 임). 그 후 "test1"값이 Topic에 할당되고 "description1"값이 Description에 할당됩니다.
모든 값 유형은 null이 아니므로 기본값과 다른 값을 가지며 참조 유형의 기본값은 null입니다.
공용 클래스 프로그램 { 공공 정적 무효 메인()는 {
// 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;
}
}
KINDLY USE MSDN http://msdn.microsoft.com/en-us/library/bb397680 .aspx # Y255 – amod
- 1. C++ 생성자 질문
- 2. 생성자 질문 (C++)
- 3. C++ 초급 질문 : 생성자
- 4. 파이썬 생성자 및 기본값
- 5. Objective-C 클래스 생성자
- 6. 기본값 : 클래스 생성자에 shared_ptr의
- 7. C++ 클래스 위임 생성자 문제
- 8. 수퍼 클래스 생성자 및 클래스 계층 구조 질문
- 9. ANSI 패딩 기본값 질문
- 10. GUID 기본값 (C++)
- 11. AS3 클래스/기본 클래스 생성자
- 12. C# 클래스 질문
- 13. C++ 파생 클래스 질문
- 14. C++ 클래스 - 포인터 질문
- 15. 싱글 톤 생성자 질문
- 16. 추상 클래스 생성자
- 17. 이동 생성자 기본 클래스 이동 생성자
- 18. 검증 C# 기본 클래스 생성자 매개 변수
- 19. 생성자 C#에서 클래스 사용 - 냄새가 나나요?
- 20. C++ 클래스, 생성자 초기화 중 값 할당
- 21. 자바 생성자 함수 초보자 질문
- 22. J2SE 질문, 생성자 자동 실행?
- 23. 상위 생성자 앞에서 하위 클래스 생성자 호출
- 24. 스칼라의 경우 클래스 생성자
- 25. 자바 하위 클래스 생성자
- 26. 추상 클래스 및 생성자
- 27. 클래스 템플릿 생성자 특수화
- 28. 콜링 기본 클래스 생성자
- 29. VectorList 클래스 생성자
- 30. Objective-C 기본 클래스 질문
이 좋은 소식입니다 에프 아니면 나. 문자열과 bool 어때? 기본값은 ""이고 false입니까? – Geraldo
@Geraldo - 아니요. 문자열은 참조 유형이므로 기본값은 'null'입니다. – Oded
문자열을 기본값으로 설정할 수있는 방법이 있습니까? 아니면 각 문자열을 지정해야합니까? – Geraldo