C# Class constructor default value question
I have the following class:
public class Topic
{
public string Topic { get; set; }
public string Description { get; set; }
public int Count { get; set; }
}
I would like to have the Count 开发者_如何学Calways set to zero when the class is created with the following:
var abc = new Topic {
Topic = "test1",
Description = "description1"
}
I am a bit confused with constructor. Is this possible or do I need to specify Topic, Description and Count when I create abc?
The default value of an int
is 0.
All value types have default values as they can't be null
.
See Initializing Value Types on this MSDN page.
You have a few different options.
1) int
defaults to zero so it will be zero if you dont initialize it.
2) you can use a constructor
public Topic(){ Count = 0;}
3) You can use a backing field instead of auto-property and initialize that to zero
private int _count = 0;
public int Count {
get {return _count}
set {_count = value; }
}
Count
will default to 0
on initialisation, since it is a value type and can't be null
.
This following idiom is not only a constructor:
var abc = new Topic {
Topic = "test1",
Description = "description1"
}
It's a constructor and an object initializer.
What really happens is that new Topic()
is called first, hence initializing all values to their defaults (property Topic is null, Description is null and Count is 0). After that, the value "test1" is assigned to Topic, and the value "description1" is assigned to Description.
All value types have a default value different than null (since they can't be null), and reference types default to null.
public class Program { public static void Main() {
// 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
}
public class 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;
}
}
EDIT
As I learned from the comments to this answer, it is perfectly valid to leave out the ()
in the initializer call.
The correct syntax would be My preferred syntax still is:
var abc = new Topic() {
Topic = "test1",
Description = "description1"
}
(note the ()
).
This would initialize Count
to 0, as 0 is the default value for int
. In case you want to always specify Topic and Description, add an explicit constructor:
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
}
精彩评论