Pages

C# Features Through the History - Version 4.0



The main features of version 4.0 now!


The version 4.0 of the language was released in the year of 2010. It was the continuation of the work for improving and providing more identity to this still new language. 

How to use the examples 

The examples are available here. You can open the solution and play around. Just uncomment the code and have fun! 



Named Arguments

With named parameters, you can specify the argument parameters by name. The order is not important anymore, although it's mandatory that all parameters being provided:

        
        public static void ExecuteExample()
        {
            // Normal order
            Method1("param1", "param2", 3);

            // It's possible to use the named parameters
            Method1(parameter1: "param1", parameter2: "param2", parameter3: 3);

            // Even inverting the order
            Method1(parameter3: 3, parameter2: "param2", parameter1: "parameter1");

            // They are strongly typed, returning exeption at compile-time
            Method1(parameter3: "asdasdasd", parameter2: 222, parameter1: "parameter1");

            // Even in different order, you should provide all parameters
            Method1(parameter3: 3, parameter2: "param2");
        }        

        public static void Method1(string parameter1, string parameter2, int parameter3) {
        }
              
            
Optional Parameters

With optional arguments, it's possible to create a method that provides a default value, in the case of this parameter is not provided. The order is important in this case, so these parameters will reside as the latest parameters, defined in the method:

        
        public static void ExecuteExample()
        {
            // Only parameter 1 is called. However, parameter2 and parameter3 have the default values
            Method1("param1");

            // Now we have a different set of values
            Method1("param1", "param2 new value", 333);

            // In this case we used named arguments. parameter2 is still fullfiled with the default value
            Method1("parameter1", parameter3: 1);
        }        

        public static void Method1(string parameter1, string parameter2 = "abc", int parameter3 = 3) {
        }
            

Dynamic Language Runtime

The DLR is a new layer on top of the Common Language Runtime. The idea is to provide the capability of solve the type of an object at runtime. This support provided a huge improvement for the .NET platform, in the sense that we could provide interoperability between both static and dynamic typed languages.  
 

Dynamic Language Runtime Architecture Overview

C# makes use of this characteristic with the dynamic type. With this new type, the compiler does not check the type at compile time, but get it at runtime:

        
                public class DynamicTest
                {
                    public static void ExecuteExample()
                    {
                        // Create the dynamic type and attribute a value
                        dynamic variable1 = 1;
                        Console.WriteLine(variable1.GetType().ToString()); //System.Int32

                        // In this case the method will receive a dynamic type
                        var dog1 = new Dog();
                        var person1 = new Person();

                        MakeItTalk(dog1);
                        MakeItTalk(person1);

                        // As you can see, it can be dangerous. You can just pass an invalid object, 
                        // that will not be infered/verified at compile-time, generating an exception at runtime
                        var amoeba = new Amoeba();
                        MakeItTalk(amoeba); // Return a RuntimeBinderException, when calling Talk() method
                    }

                    public static void MakeItTalk(dynamic animal) {
                        animal.Talk();
                    }       
                }

                public class Dog {
                    public void Talk() {
                        Console.WriteLine("Wolf, wolf!");
                    }
                }

                public class Person {
                    public void Talk() {
                        Console.WriteLine("Hey you!");
                    }
                }    

                public class Amoeba {}        
            

One observation is that dynamic type differ totally from the 'var' keyword. With var, the type is solved at compile-time.

Covariance and Contravariance for Generic Types

Some concepts are important to understand this point:

- Covariance: enables you to use a more derived type, instead of the original one;

- Contravariance: it's the opposite of covariance, allowing for a more generic type in the place of a more specific one;

- Invariance: you can use only the original type specified. It's neither covariant nor contravariant.

The C# 4.0 provided some interfaces that have covariant type parameters (e.g. IEnumerable<T>, IEnumerator<T>, IQueryable<T>, IGrouping<TKey, TElement>). These types can be used only for the return types of the members.

        
    public class CovariantContravariantGenericTypesTest
    {
        public static void ExecuteExample()
        {
            // It'll work because now, IEnumerable supports covariance
            IEnumerable mg = GetManager();
            IEnumerable em = mg;
            
            // List is not covariant in T
            List mg2 = new List();
            //List em2 = mg2; // CS0029: error of implicit conversion
        }

        public static IEnumerable GetManager() {
            IEnumerable manager = null;

            return manager;
        }
    }

    public class Employee {
    }

    public class Manager : Employee {
    }
            

On the other hand, some generic interfaces have contravariant type parameters (e. g. IComparer<T>, IComparable<T>, IEqualityComparer<T>).


Minor features related to COM services

This version added minor features in the way to integrate with COM components: 

- Indexed Properties: C# 4.0 started to support indexed properties on COM interop types;
Omission of the ref keyword at COM call sites;
- Embedding COM interop services.

Fabio Ono

No comments:

Post a Comment