VS2015 new features in preview c#6.0 (ii)

Source: Internet
Author: User

VS2015 new features in preview c#6.0 (i)

VS2015 new features in preview c#6.0 (iii)

Enhancements to Automatic attributes

    • Read-only automatic properties

Previously automatic attributes must provide both setter and Getter methods, so read-only properties can only be implemented by declaring the field first, and then the property provides only getter methods, which cannot be implemented by automatic attributes. In c#6.0, you can declare a read-only automatic attribute in the following form:

 Public string get; }

For a read-only automatic attribute, its backing field is ReadOnly, and its value can be given in the property initializer (the property initializer is explained in detail below) or in the constructor of the class. In the following example, the FirstName and LastName properties are assigned by an automatic property initializer, and Timeofstartingwrite is assigned by a constructor.

 Public classauthor{ PublicDateTime Timeofstartingwrite {Get; }  Public stringFirstName {Get; } ="Dery";  Public stringLastName {Get; } ="Xu";  PublicAuthor (DateTime timeofstartingwrite) {timeofstartingwrite=Timeofstartingwrite; }}

Because a read-only property does not have a setter method, its value is passed directly to its backing field.

    • Automatic property initializer

In the example above you can see that for automatic attributes, we can use the property initializer to assign a value to it. Examples are as follows:

  Public classBook { Public intNumber {Get;} = -;  Public stringAbstract {Get;Set; }="This is abstract";  Public stringName {Get;Set; }  Public floatPrice {Get;Set; }  PublicAuthor Primaryauthor {Get;Set; }  PublicList<author> Authors {Get;Set; } }

Where number is a read-only automatic attribute, we assign it a value of 100 through the property initializer, and abstract is a read-write automatic property that we assign to the string "This is abstract" through the property initializer. The automatic property initializer is assigned directly to the field generated in the background and does not walk the setter method of the automatic attribute. It has the following characteristics as the field initializer:

    1. Executes in the written order, so in the previous example, the initialization of the number property is performed before the initialization of the abstract property
    2. This cannot be used because it is run before the object is fully initialized

The value that can be assigned to an attribute in an automatic property initializer seems to be limited, and the following main constructor helps it to get more likely values.

Main constructor

The main constructor function merges the declaration of the constructor into the declaration of the class. Let's write the code that looks like this:

[Serializable] Public classPatent (stringTitlestringyearofpublication) {   PublicPatent (stringTitlestringyearofpublication, IEnumerable<string>inventors): This(title, yearofpublication) {inventors.addrange (inventors); }  Private string_title =title;  Public stringTitle {Get        {            return_title; }        Set        {            if(Value = =NULL)            {                Throw NewArgumentNullException ("Title"); } _title=value; }  }}

However, this feature is not supported in this release and will be further explained in a later update.

Expression-body functions and properties

For lambda expressions, we know that you can declare it either through an expression body or a normal function in a block of statements, which can now be applied to a function member of a class.

    • Function members
 Public int ADD (intint op2) = = OP1+ publicvoid Print (string message) = > Console.WriteLine ("" + message);

The effect of using an expression body function is the same as a function body with only a single return statement, so the above example is the same as the following code

 Public int ADD (intint  op2) {       return OP1 + op2;}  Public void Print (string  message) {      Console.WriteLine ("" +  message);}

Note that the Add method has a return value, and print is not a return value, and for a function that has no return value, the lambda expression body must be a statement lambda that is new, call, Decrement,increment, assignment, and so on, rather than an expression that expresses the complete statement. This requirement is the same as for lambda.

    • Properties and indexers

Properties and indexers have getter and setter methods, expression body can be used to write read-only properties and indexers, the expression body is the method of the getter method body, examples are as follows:

 Public  This [int id] = store[id];  Public string FullName = FirstName + lastName;

Note that there is no need to give the Get keyword, and the compiler makes an implicit inference.

Complete examples are as follows

Internal classExpressionbody {Private stringFirstName ="First name"; Private stringLastName ="LastName"; PrivateList<author> store =Newlist<author> {NewAuthor (DateTime.Now),NewAuthor (DateTime.Now)};  Public intADD (intOP1,intOP2) = Op1 +OP2;  Public voidPrint (stringmessage) = Console.WriteLine ("Hello"+message);  PublicAuthor This[intID] = =Store[id];  Public stringFullName = FirstName +LastName; //public int Add (int op1, int op2)//{        //return OP1 + op2; //}        //Public void Print (String message)//{        //Console.WriteLine ("Hello" + message); //}}

VS2015 new features in preview c#6.0 (ii)

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.