Microsoft does it again! With .Net Framework 4 Microsoft takes the C# language to new levels. In this session we will learn how to write better code using Dynamically Typed Objects, Optional & Named Parameters and Co-variance and Contra-variance.
3. The Evolution of C# Dynamic C# 4.0 LINQ C# 3.0 C# 2.0 Generics C# 1.0 Managed Code
4. Dynamically Typed Objects Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); object calc = GetCalculator(); TypecalcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, newobject[] { 10, 20 } ); int sum = Convert.ToInt32(res); Statically typed to be dynamic dynamic calc = GetCalculator(); int sum = calc.Add(10, 20); Dynamic method invocation Dynamic conversion
5. The Dynamic Type Resolved only at runtime. object dynamic Any object can be implicitlyconverted to dynamic. dynamic object Any dynamic Type can be assignment conversion to any other type. dynamic x = 1; // implicit conversion int num = x; // assignment conversion
10. Optional & Named Parameters Optional parameters publicStreamReaderOpenTextFile( string path, Encodingencoding, booldetectEncoding, intbufferSize); publicStreamReaderOpenTextFile( string path, Encodingencoding = null, booldetectEncoding = true, intbufferSize = 1024); Named argument OpenTextFile("foo.txt", Encoding.UTF8); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Arguments evaluated in order written Named arguments can appear in any order Named arguments must be last OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false); Non-optional must be specified
13. Covariance (Out) Covariance (Out):Is the ability to use a more derived type than that specified. delegateAnimal MyDel(); publicDogMethod() { ... } MyDeldel = Method; // Co - Variance (Out): Return Dog as Animal, Ok. Animalanimal = del();
14. Contra Variance (In) Contra variance (in):Is the ability to use a less derived type delegatevoidMyDel( Dogdog); publicvoidMethod(Animalanimal ){ ... } MyDeldel = Method; del( newDog() ); // Contra-Variance (In): Arg Dog as Animal, Ok.
15. Covariance & Generic What you think? IList<string> strings = new List<string>(); IList<object> objects = strings; objects[0] = 5;strings = strings[0];
16. publicinterfaceIEnumerable<T> { IEnumerator<T> GetEnumerator(); } publicinterfaceIEnumerable<out T> { IEnumerator<T> GetEnumerator(); } publicinterfaceIEnumerator<T> { T Current { get; } boolMoveNext(); } publicinterfaceIEnumerator<out T> { T Current { get; } boolMoveNext(); } Safe Covariance (Out) out= Co-variantOutput positions only Can be treated asless derived IEnumerable<string> strings = GetStrings(); IEnumerable<object> objects = strings;
17. in= Contra-variantInput positions only publicinterfaceIComparer<T> { int Compare(T x, T y); } publicinterfaceIComparer<in T> { int Compare(T x, T y); } Safe Contra Variance (In) Can be treated asmore derived objectx, y; IComparer<object> objComp = GetComparer(); objComp.Compare( x , y ); string x1, y1; IComparer<string> strComp = objComp; strComp.Compare( x1 , y1 );