C #3.0 New Features

Source: Internet
Author: User

1. Automatic attributes

The following two statements share the same purpose

public int Age { get; set;}

  

private int Age;public int Age{  get { return age; }  set { age = value; }}

// The difference between the above two statements is equivalent to that between China and the People's Republic of China.

2. Empty type

For the value type, the specific value must be defined at the same time; otherwise, compilation may fail. however, in some cases, users do not know the value in advance, for example, they cannot know the student's age in advance. it is important to define an age as a type that can be null.

  1. Value types include: int \ double \ float \ decimal \ bool \ char \ sturct
  2. The syntax for null is: nullable <t>. t indicates a specific type above. For example, it can be an empty int type: nullable <int>; it can be an empty bool type: nullable <bool>
  3. A variable that can be empty. When being assigned a value, null can be assigned to it, for example, nullable <int> age = NULL;
  4. Nullable <t> can be abbreviated as t? Example: Int? Age = NULL; equivalent to nullable <int> age = NULL;
  5. The following two statements are incorrect, because both string and student are reference types.
    1. String? Name = NULL;
    2. Student? Stu = NULL;
  6. Common properties that can be empty:
    1. Hasvalue: boolean type. If the field is not null, true is returned. Otherwise, false is returned.
    2. Value: gets the value of a field. If the value of this field is null, an error is returned when the value attribute is called.

 

3. Generic Type

  1. List <t>: T can refer to any type, for example, int \ bool \ string \ student \...
  2. Dictionary <t, k>: dictionary class, where T and K can refer to any type
  3. Keyvaluepair <tkey, tvalue>: Key-Value Pair type

4. Object Initiator

Example:

Student Stu = new student () {name = "Zhang San", age = 18 };

  

5. Set Initiator

Example:

List <student> stulist = new list <student> () {Stu,
New student () {name = "", age = 20}
};

  

6. var keywords

  1. VaR is used to define variables. The types of variables are determined by values.
  2. VaR A = 5; Because 5 is of the int type, the type is also of the int type.
  3. VaR Stu = new student (); because the student type variable is created through new, Stu is of the student type.

Note 1: var B; this method is incorrect because the variable declared by VAR must be initialized at the same time as the definition.

NOTE 2: Once a variable declared through VaR is instantiated, its data type cannot be changed. For example, the following code:

VaR A = 5; A = "zhangsan"; // A = "zhangsan" error

7. Anonymous type

When instantiating a class object, you can dynamically create class objects through new {} without having to define the class in advance.

Example:

VaR preson = new {name = "Wang Wu", sex = "male "};

 

8. Expansion Method

Extends other methods for previously defined classes.

  1. First define a static class (the class name is unlimited)
  2. Define a static method in a static class (the method return value is customized according to the actual situation)
  3. Add a parameter to the above static method, which must be modified by the this keyword.
  4. The above parameter type is the extended type of the extension method.

Example: Student Extension Method

Public static class studentextention {public static string consolestudent (this student Stu) {If (! Stu. age. hasvalue) {Stu. age = 18;} string STR = "name:" + Stu. name + "\ n" + "Gender:" + Stu. sex + "\ n" + "Age:" + Stu. age; return STR ;}}

  

9. Anonymous type and Lambda expression

  1. Delegate: A delegate allows a method to be passed as a parameter to another method.
  2. Delegate and mount methods: the number of parameters \ parameter Type \ Parameter order \ return value type must be consistent.

Example:

 

 

However, the above method is complicated to use. We can use the anonymous method to abbreviated the above features. Lambda expressions are also a quick way to write anonymous methods.

Lambda expressions are divided into three parts: parameters |=> | expressions

The following code demonstrates an example of using a Lambda expression to rewrite a delegate.

 

Note 1: If the lambda expression has only one parameter, the parameter does not need to be enclosed ().

Note 2: The Lambda expression can be wrapped in {} or not. If there are multiple lines of code, you must wrap it in.

Therefore, where the delegate (func parameter) occurs, we can write lambda expressions.

 

Func <t, k> is a generic delegate. The parameter type of this delegate is t, and the return value type is K.

The ARG type of the lambda expression parameter is t, and the partial return value type of the lambda expression is K.

10. Division

Keyword: Partial

Partial is a class modifier used to split the class definition into several parts for easy code management.

 

Ending ......

 

 

 

C #3.0 New Features

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.