One: Difference of value type/reference type
Value types mainly include simple types, enumeration types, and struct types, and so on, instances of value types are usually assigned to the thread stack above the contents of the variables stored in the instance data itself. The reference type is allocated on the managed heap, and the variable holds the address. Reference types mainly include class types, interface types, delegate types, string types, and so on.
For parameter passing, there are four types:
Value-type parameters are passed by value;
The reference type parameter is passed by value;
The special case of string reference type parameters passed by value, although the string type is also a reference type, but when passed by value, the passed argument is not modified by the change of the parameter in the method.
Class program { static void Main (string[] args) { Console.WriteLine ("Special Case of string reference type passed by value"); String str = "Old string"; Reference type changestr (str); Console.WriteLine ("Value after call:" +str); Console.readkey (); } private static void Changestr (string oldstr) { oldstr = "New string"; Console.WriteLine ("Oldstr in Method:" +oldstr);} }
This is due to the fact that the string has invariance, once a string type is assigned, then it is not changed, that is, the value cannot be modified by the code, it seems that Str has been modified, but because of the immutability of the string type, A chunk of memory space is reassigned to hold the new string string. The modified memory header address is assigned to the OLDSTR variable, so the value is changed.
The value type parameter is passed by reference;
The reference type parameter is passed by reference;
Here you need to use the two keywords of ref,out, which are passed by reference for both the value type and the reference type.
Second: Re-understanding------generics
Generics (generic): Is a generic type, it can replace any data type, is a type parameterization, so as to achieve only one method can operate a variety of data types. Generics separate the data types of the method implementation behavior and method operations to implement code reuse.
Initializes the generic type list<int> inlist=new list<int> () with int as the actual parameter; Inlist.add (3); Initializes a generic type list<string> instring=new list<string> () using string as the actual parameter; Instring.add ("Ahui");
The above is that the generic,list<t> is a generic type implemented in the. NET class library, T is a generic parameter (formal parameter), and if you want to instantiate a generic type, you must pass in the actual type parameter.
<summary>/// define a generic---compare the sizes of various types. IComparable interface is because there is a CompareTo method///</summary>// <typeparam name= "T" > generic parameter, is the type of the pass-through </ Typeparam> public class compare<t> where t:icomparable {public static T Comparegeneric (T t1,t T2) { if (T1.compareto (T2) >0) { return t1; } else { return t2;}} }
The T in this is the type (the generic type parameter) that we pass in the future when we call the method. Comparegeneric is a method that implements generics, where statements in code are constraints on type parameters that enable type parameters to be applied to the CompareTo method. The type parameter is thus constrained.
Call generics, the direct point operation can come, only need to pay attention to the type passed in. Console.WriteLine (Compare<int>. Comparegeneric ()); Console.WriteLine (COMPARE<STRING>. Comparegeneric ("2222", "111")); Console.readkey ();
1: Generics provide better performance and type-safety features in addition to code reuse.
2: Use generics to reduce the performance cost of boxing and unpacking. Because we are directly a type, we do not need to convert it to the object type before you can manipulate it.
3: In generic code, T is a type parameter, which requires a real type instead of T, regardless of the invocation of the type method or the initialization of the generic instance.
4: We sometimes do not need to assign a value to T, the compiler can itself to determine what type T is, this is the inference of the type parameter. (Parameters can only be one type)
5: Constraint of type parameter:
There are 4 constraints, similar syntax: Constraints are placed at the end of a generic method or type declaration, and you want to use the WHERE keyword.
---> Reference type constraints
The representation of a reference type constraint is T:class, which ensures that the type argument passed must be a reference type.
<summary>///reference type constraint///</summary>// <typeparam name= "T" ></typeparam> Public class Samplereference<t>where T:stream {Public void Test (T stream) { stream. Close (); } }
The type parameter T sets the reference type constraint. Where t:stream means to tell the compiler that the type to be passed must be System.IO.Stream or its subclasses.
---> Value type constraints
Representation: T:struct
<summary>///value type constraint///</summary>// <typeparam name= "T" ></typeparam> Public class samplevaluetype<t> where t:struct //constraint is struct, (value type) {public static T Test () { return new T (); T is a value type, and all value types have a common parameterless constructor, } }
---> Constructor type constraints
Representation: T:new (), if there are multiple constraints this constraint should be specified last.
---> Conversion type constraints
Representation: T: base class name. T: Interface name or t:u;
----> Combination constraints
is to combine the above, the class must precede the interface, and the different type parameters can have different constraints, but each type parameter must use a separate where keyword.
Three: Nullable types
A nullable type is also a value type, but it contains a value type of a null value.
Int? A=null;
int? is the nullable int type,? Modifiers are just C # 's syntactic sugars, which is a handy representation of C #.
Four: Empty merge operator (??)
?? operator, it will judge the left and right two operands, and if the left number is not NULL, the left number is returned. If the number on the left is null, the number to the right is returned. It is used primarily for nullable types, and can also be used for reference types, but not for value types.
///<summary>///?? Operator//</summary> private static void Nullcoalescingoperator () {int? nullable = Nu ll Int? Nullhasvalue = 1; int x = nullable?? 12; Same as the three-mesh operator function. int y = nullhasvalue?? 123; Console.WriteLine ("Nullable type has no value:" +x); Console.WriteLine ("Nullable type has value:" + y); Console.WriteLine (); //?? Shipped in reference type string stringnotnull = "123"; string stringisnull = null; string result = Stringnotnull?? "456"; String result2 = Stringisnull?? "12"; Console.WriteLine ("Case with reference type NOT NULL:" +result); Console.WriteLine ("Case with reference type null:" + RESULT2); }
static void Main (string[] args) {
Console.WriteLine (); Nullcoalescingoperator (); Console.readkey ();
}
Use?? Operators make it easy to set default values, avoiding the ability to judge by if,else statements, simplifying the number of lines of code and improving the readability of the code.
Five: Anonymous method
Explanation: There is no name, because there is no name, the anonymous method can only be used when the function definition (that is, the definition and implementation of the method is nested together). cannot be called under any other circumstances.
A delegate is a precondition for an anonymous function.
public class Friend { ///<summary>///How to implement the delegate (the method to be passed by the delegate)///</summary>// < Param name= "nickname" ></param> public void Vote (string nickname) { Console.WriteLine ("Nickname: {0} , Come on ", nickname); } }
Class program { //define delegate private delegate void Votedelegate (string name); private static void Main (string[] args) { //instantiation of Delegate object votedelegate votedelegate = new Votedelegate (New Friend (). Vote); The vote method is passed as a parameter. votedelegate ("Ahui");//Ahui passed to the following method. Console.readkey (); } }
The following is the use of anonymous functions
Class program { //define delegate private delegate void Votedelegate (string name); private static void Main (string[] args) { //Use an anonymous function to instantiate a delegate. Must be a delegate. The rest is the same as the method. votedelegate votedelegate = Delegate (string nickname) { Console.WriteLine ("Nickname:" + nickname); }; The vote () method is called back and forth by calling the delegate, which is an implicit invocation. votedelegate ("Ahui"); Console.readkey (); } }
We use anonymous functions without having to define a separate vote method, which reduces the number of lines of code.
Disadvantages of anonymous functions:
Can not be called by other places, easy to form a closure.
30 Days C # Foundation Consolidation-----Value type/reference type, generics, empty merge operator (??), anonymous method