What is new in C# 4.0

News in .NET 4 and Visual Studio 2010 release wave – part three

In new version of C# are main features to support of dynamic languages and DLR interop and also better support for earier COM interop.

  1. Dynamic keyword to Support for dynamic languages. Dynamic allows to specify non-strict type and compiler will make any complain.

    image 
    Picture: IntelliSense will make no advises on dynamic object which are not strongly typed.

    Ability of .NET Framework to call a method at runtime instead of binding at compile time is called Dynamic lookup and is provided by DLR runtime.

  2. Dynamic lookup – allows calls using late-binding or run-time method calls.

    This approach allows developer better interop with COM, or, it will enable dynamic languages, like Ruby or Python interoperability. Developers can now use for example Python runtime:

    ScriptRuntime py = Python.CreateRuntime();
    dynamic random = py.UseFile("random.py");
    //Make an array of numbers
    var items = Enumerable.Range(1, 7).ToArray();
    random.shuffle(items);

  3. Named and optional arguments – Allows not to specify argument when not needed or specify argument with a name
    Follows the sample of using optional arguments in C#:

    static void Main(string[] args)
    {
      var testClass = new NewType();
      testClass.OptionalTest();
      testClass.OptionalTest(2,"Privet");
      Console.ReadLine();
    }
    class NewType
    {
      public void OptionalTest(int p1 = 1,
          string s1="Hello") {
        Console.WriteLine(p1 + s1);
      }
    }

    And second example uses optional and named arguments:

    static void Main(string[] args)
    {
      var testClass = new NewType();
      testClass.NamedTest(p1:3, s1:"Grus");
      Console.ReadLine();
    }
    class NewType
    {
      public void NamedTest(int p1 = 1, string s1="Hello") {
        Console.WriteLine(p1 + s1);
      }
    }

  4. Variance – Compiler allows to specify types from type hierarchy in interfaces and typed collections
  5. In-process side by side execution – allows to run multiple framework versions in one process
  6. New class Tuple – type known from functional languages, ordered set of N values can be used to store up to 8 values into one type
  7. Type equivalence support – Embedding PIA (Primary Interop Assemblies) directly In your assembly

 

This is series of articles News in .NET 4 and Visual Studio 2010 release wave

3 thoughts on “What is new in C# 4.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s