C # 3.0 Get Set default value __c#

Source: Internet
Author: User

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; }    }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.