Some small knowledge of C #

1.var keyword

var is a weak type, any type may be substituted

It has the following four characteristics

1. must be initialized in the definition

var s="adcd";  //可以
var s;
s="abcd";  //不可以

2. roar initialization is complete, which can not be assigned to different types of values

Local variables required 3.var

4. var define variables and different object, it is strongly typed manner defined and used in exactly the same variable efficiency.

And out of the difference between 2.ref

NOTE: In C #, parameter passing method There are four types: traditional values ​​(by value), transmission site (by reference), the output parameter (by output), an array of parameters (by array). By value parameters without additional modifiers, pass-parameters required modifier ref, output parameters required modifier out, an array of parameters required modifier params.

1, when using the ref-type parameters, the parameters passed must be initialized. On out, it must complete its initialization process.

2, when the ref and out, in the execution parameters and methods, should add Ref or Out keywords. To meet the match.

3, out for use where necessary retrun multiple return values, and the ref is used in the method needs to be called when referring to modifications of the caller.

The difference between the two: ref there is a carry out, out is out without.

3. commissioned

Declare a delegate

//delegate <return type> <delegate-name><parameter list>
public delegate int MyDelegate (string s);

Examples of the commission

public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);

Multicast delegate

Use the "+" operator to merge a merge two delegate delegate calls its merger. Only the same type of trust can be merged

 // 创建委托实例
         NumberChanger nc;
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         nc = nc1;
         nc += nc2;
         // 调用多播
         nc(5);
   //调用nc时会同时调用nc1和nc2

4.foreach does not support adding delete operations in a loop

Guess you like

Origin www.cnblogs.com/Ligo-Z/p/11256405.html