Language Enhancements In C# 3.0 and VB 9

There are so many new features added as Language Enhancement for C# 3.0 and VB 9.0 in Visual Studio 2008 (along with .NET Framework 3.5). Here, I’m compiling a quick list of important ones and will share some resources from where you can learn more about the same.

  1. Local Type Inference
    Visual Studio uses type inference to determine the data types of local variables declared without data-type. The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type, as shown in the following example.
    String str1 = "Mayur Tendulkar";
    var str2 = "Mayur Tendulkar";

    For more information visit following links:
    http://msdn2.microsoft.com/en-us/library/bb384937.aspx

  2. Object and Collection Initializers
    Object and Collection Initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor
    class Person
    {
    String Name { get; set; }
    String EMailID { get; set; }
    }
    class PersonImplementation
    {
    static void CreatePerson()
    {
    //Object Initializer Used
    Person p = new Person { Name = "Mayur Tendulkar", EMailID = "mayur.tendulkar@gmail.com" };
    //Collection Initializer (with Object Initializer) Used
    List per = new List
    {
    new Person() { Name = "ABC", EMailID = "abc@abc.com"},
    new Person() { Name = "XYZ", EMailID = "xyz@xyz.com"}
    };
    }
    }

    For more information, visit following link:
    http://msdn2.microsoft.com/en-us/library/bb384062.aspx

  3. Anonymous Types
    Anonymous types enable you to create objects without writing a class definition for the data type. Instead, the compiler generates a class for you. The class has no usable name, inherits directly from Object, and contains the properties you specify in declaring the object. Because the name of the data type is not specified, it is referred to as an anonymous type.
    var Student = new { Name = "Mayur", LastName = "Tendulkar"};

    For more information, visit following link:
    http://msdn2.microsoft.com/en-us/library/bb397696.aspx

  4. Auto Implemented Properties
    Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field can only be accessed through the property’s get and set accessors.
    class Female
    {
    string Name { get; set; }
    int Age { private get; set; } //Write only
    int Mobile { get; private set; } //Read only
    }

    For more information visit following link:
    http://msdn2.microsoft.com/en-us/library/bb384054.aspx

  5. Partial Method
    Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented.
    // Definition in file1.cs
    partial void onNameChanged();

    // Implementation in file2.cs
    partial void onNameChanged()
    {
    // method body
    }

    For more information visit following link:
    http://msdn2.microsoft.com/en-us/library/wa80x488.aspx

In this blog post we seen 5 new concepts that are included in VS2008 (.NET 3.5) and in next post, we’ll see some more of them.

Advertisements

Published by

Mayur Tendulkar

Working as a Program Manager for Xamarin in Microsoft. More details at: https://about.me/tendulkar

One thought on “Language Enhancements In C# 3.0 and VB 9”

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