1. Optional parameters and named parameters
When you design a parameter, you can assign default values for some or all of the parameters, and the code that invokes the methods can choose not to specify partial arguments, accept the default values, and pass the arguments by making the parameter names. As follows
classclr Optional parameter {Private Static intS_n =0; Private Static voidMintx =9,strings ="A", DateTime dt =default(DateTime), GUID guid =NewGuid ()) {Console.WriteLine ("X={0},s={1},dt={2},guid={3}", x, S, DT, GUID); } Public Static voidTest () {//equivalent to M (9, "A", Default (DateTime), New Guid ())M (); //equivalent to M (8, "X", Default (DateTime), New Guid ())M8,"X"); //equivalent to M (5, "A", DateTime.Now, Guid.NewGuid ()))M5, Guid:Guid.NewGuid (), Dt:DateTime.Now); //equivalent to String t1=0,t2=1; //M (T2,t1,default (DateTime), New Guid ())M (S: (s_n++). ToString (), x:s_n++); } }
Results:
X=9,S=A,DT=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000
X=8,S=X,DT=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000
X=5,S=A,DT=2014/10/20 1:23:02,guid=ae35fafd-dc14-43d9-be4c-97e7e33d0cf6
X=1,S=0,DT=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000
2. Implicitly-typed local variables
That is, using Var, the compiler infers the specific data type based on the expression. Just a simplified syntax for reporting variables that can only be used to declare local variables inside a method. There's not much to say,
3. Passing parameters (ref and Out keywords) to the method in a quoted manner
The CLR passes a parameter in the form of a keyword ref and out to pass a reference rather than a value, telling the compiler to generate code to pass the address of the parameter instead of passing the parameter itself.
In the CLR's perspective, ref and out are exactly the same, except that an out-of-date initialization object is not required, and that a value must be written before it is returned, whereas the REF keyword must initialize the value of the parameter before calling the method.
Public Sealed classprogram{ Public Static voidMain () {intX//x not initializedGetval ( outx);//x does not have to be initializedConsole.WriteLine (x);//Show Ten } Private Static voidGetval ( out intv) {v=Ten;//The method must initialize the V } } Public Sealed classprogram{ Public Static voidMain () {intx=5;//already initializedGetval (refx);//x does not have to be initializedConsole.WriteLine (x);//Display } Private Static voidGetval (ref intv) {v+=Ten;//the method can use the initialized value of v } }
4. Passing a variable number of parameters (params) to a method
Static intADD (params int[] values) { intsum=0; if(values!=NULL) { for(intx=0; x<values. length;x++) Sum+=Values[x]; }}//Calling Method 1ADD (New int[]{1,2,3,4,5,6});//calling Method 2, simplifying the methodADD (1,2,3,4,5,6);
Only the last parameter of the method can be tagged with the params keyword, but using that keyword can cause some additional performance damage because the array object must be allocated on the heap, must be initialized, and eventually garbage collected. You might consider defining several overloaded versions that do not use the params keyword.
5, guidelines for defining parameters and return types
1. Try to specify the weakest type, preferably an interface rather than a base class
Good: Use a weak parameter type
public void Manipulateitems<t> (Ienumerable<t> collection) {}
Bad: Use a strong parameter type
public void Manipulateitems<t> (List<t> collection) {}
The first method is called to pass an array object, an List<t> object, a String object, and other objects that implement the IEnumerable interface. The second method only allows the List<t> object to be passed, obviously the first one is more flexible.
2, a good return type is generally declared as the strongest type to avoid being limited to a particular type.
OK, return strong return type
Public FileStream OpenFile () {}
Bad, returns the weak return type
Public Stream OpenFile () {}
Because the first method can either return FileStream or return a stream
"CLR in C #" parameter