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.
- Value types include: int \ double \ float \ decimal \ bool \ char \ sturct
- 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>
- A variable that can be empty. When being assigned a value, null can be assigned to it, for example, nullable <int> age = NULL;
- Nullable <t> can be abbreviated as t? Example: Int? Age = NULL; equivalent to nullable <int> age = NULL;
- The following two statements are incorrect, because both string and student are reference types.
- String? Name = NULL;
- Student? Stu = NULL;
- Common properties that can be empty:
- Hasvalue: boolean type. If the field is not null, true is returned. Otherwise, false is returned.
- 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
- List <t>: T can refer to any type, for example, int \ bool \ string \ student \...
- Dictionary <t, k>: dictionary class, where T and K can refer to any type
- 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
- VaR is used to define variables. The types of variables are determined by values.
- VaR A = 5; Because 5 is of the int type, the type is also of the int type.
- 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.
- First define a static class (the class name is unlimited)
- Define a static method in a static class (the method return value is customized according to the actual situation)
- Add a parameter to the above static method, which must be modified by the this keyword.
- 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
- Delegate: A delegate allows a method to be passed as a parameter to another method.
- 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