Oct 4, 2008

What’s New in Visual C# - Part 1

note: Part II added at link http://mstarek.blogspot.com/2008/10/whats-new-in-visual-c-part-2.html

In last days I read about the new C# 2008 features, and I decide to share my little knowledge by writing this article,

In this article I will try to summarize the main new features in Visual C # 2008 language,

  1. implicitly typed Local variable :

    it's the "var" keyword, we used to use it in JavaScript only, but it's new here in c#2008, it's not type in its own but it instruct the compiler to set type to this variable according to its initialization.
    For Example:

    var i = 5; //the compiler will infer that this variable is integer.

    // s is compiled as a string

    var s = "Hello";


    // a is compiled as int[]

    var a = new[] { 0, 1, 2 };

  2. Collection initialization :
    in new C# you can initialize the collection object while you declare it , for Example:

    // Object initializer

    Employee emp1 = new Employee { Age = 10, Name = "Tarek" };

  3. Extension Methods:

    Really it's a very good enhancement here, with "Extension Methods" you can extend the known classes and types functions , for example you can add new function on string class to use it easly from your code, and so, this is the syntax of that

    public
    static
    class
    MyExtensions

    {

    public
    static
    int WordCount(this
    String str)

    {

    return str.Split(new
    char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;

    }

    }

    After that you will find this method as extension method on any string.

  4. Anonymous Types:

    To simplify the logic of Anonymous types, we can say it's typed objects, but its type can't be shown within code tyme, for Example:
    this statement make a new typed object with two properties, "Amount" is integer property and "Message" is string property,

    var v = new { Amount = 108, Message = "Hello" };

    this way is useful when we need a typed object to be used in small area of code, then we don't need to make a new class with this type to be used, just make this object and the comiler will make this class to you at runtime (and assign new name to it), its simple and will be used in LINQ.


We will continue the new C# features in Part 2 ISA,

note: Part II added at linkhttp://mstarek.blogspot.com/2008/10/whats-new-in-visual-c-part-2.html

1 comment:

El-Mallah - الحمد لله said...
This comment has been removed by a blog administrator.
nice web tools English Definitions and Dictionary dutch definition and Dictionary , Nederlands definitie finnish definition and Dictionary, hollantilainen sanakirja French definition and Dictionary, le dictionnaire Français arabic definition and Dictionary, قاموس اللغة العربية hindi definition and Dictionary, शब्दकोश, हिन्दी शब्दकोश bengali definition and Dictionary, বাংলা অভিধান portuguese definition and Dictionary, dicionário de Português urdu definition and Dictionary، اردو لغت russian definition and Dictionary, русский словарь spanish definition and Dictionary, diccionario de español