The C # 3.0 {get; set;} defaults have been introduced before, and this article is based on it and adds some content.
The. NET Framework 3.5 uses C # 3.0,c# 3.0 with some new language features, one of which is the shortcut properties.
Previous wording: private int _id = 0;
public int Id
{
Get
{
return _id;
}
Set
{
_id = value;
}
}
This can be abbreviated in C # 3.0:
public int Id {get; Set }
C # 3.0 {get; set;} Default value
This has to say {get; set;} 's default value, because there is no private from the paragraph, we can not manually specify the default value, then the system's default value is. for type int, the default value is 0, for int? Type, the default value is NULL, and the default value is FALSE for bool type, and for bool? Type, the default value is null, and for string type, the default value is null, and for string? Type, haha, there is no such wording, there will be errors; for datetime types, the default value is 0001-01-01 00:00:00; for datetime? Type, the default value is null, and for the enum type, the default value is an item with a value of 0, and if there is no 0 enum entry, it is still 0, as can be seen in the following: C # enumeration (enum); Type, the default value is null; for class type, the default value is an unreferenced object reference; for class? Type, haha, there is no such wording, there will be mistakes.
About type addition, which means that the value of this type can be null, such as an int with no null value, plus int? can be null.
[CSharp] View Plain copy public string test { get { if (test == null) { return "Default value"; } else { return test; } } set { test = value; } }