1. Name parameters and optional parameters
Static void print (string enname, cnname = "", int age = 25 ){...} print ("prime li"); print ("prime li", "Li Fei"); print ("prime li", "Li Fei", 25); print (enname: "prime li"); print ("prime li", cnname: "Li Fei", age: 25 );
Rules:
1. Non-optional parameters must be declared first.
2. Non-optional parameters must be assigned a value during the call.
3. parameters are declared in the order.
4. If the two signatures are the same, non-optional parameters will be given priority.
Ii. Out and in)
We recommend that you read generic covariant and reverse variant in. Net 4.0.
In 3.5, the followingCodeCompilation fails.
Ilist <elephant> elements = new list <elephant> (); ienumerable <animal> animals = elephants;
But in 4.0, we can write as follows:
Define the Ibar <out T1, in T2> interface to securely convert Ibar <string, Object> to Ibar <object, string>
Note:
1.Only generic interfaces and generic delegation support the variability of type parameters. generic methods are not supported.
2.Value types do not participate in covariant or reverse variant,Ifoo <int>Never becomeIfoo <Object>No matter whether it has been declaredOut. Because. NetGeneric. Each value type generates an exclusive closed construction type, which is incompatible with the reference type version.
3.When declaring an attribute, you must note that the type of the read/write attribute is used for both parameters and return values. Therefore, only read-only attributes are allowed.OutType parameter, which can be used only for writing attributesInParameters.
Iii. Dynamic Language
1. Dynamic type
Dynamic mydynamic = "string"; console. Write (mydynamic. GetType (). Name); // "system. String"
2. Dynamic! = VaR
Dynamic is run time and VaR is compile time. Therefore, dynamic cannot intelligently prompt, while VaR can.
3. Replace reflection with dynamic
Dynamic usingdynamic = activator. createinstance (type. getType ("system. text. stringbuilder "); usingdynamic. append ("Hello dynamic"); console. write (usingdynamic. tostring ());
4. system. Dynamic. expandoobject
Expandoojbect is a class that can dynamically/delete attributes, methods, and events at runtime.
Dynamic myexpando = new expandoojbect (); myexpando. value1 = "new value 1"; myexpando. value2 = "New Value 2"; myexpando. dosomething = new action () => console. write ("method call"); console. write (myexpando. value1 );
5. system. Dynamic. dynamicobject
Class program {static void main (string [] ARGs) {dynamic easierxml = new easierxml (@ "<Test> <node1> alpha </node1> <node2> beta </node2> </test>"); console. writeline (easierxml. node1); console. writeline (easierxml. node2); console. readkey ();} public class easierxml: dynamicobject {private xdocument _ xml = new xdocument (); Public easierxml (string XML) {This. _ xml = xdocument. parse (XML);} public override bool trygetmember (getmemberbinder binder, out object result) {string nodename = binder. name; Result = _ xml. element ("test "). element (nodename ). value; return true ;}}}