New features in C# 3.0 (“C# Orcas”)

This page is to list out the new features in c# 3.0.

let’s go through all the features one by one

  1. Implicitly typed local variables

    If a local variable declaration defines “var” as a type and there is no class called “var” is defined in the scope, then it defines the variable to be implicitly types.
    (reminds me of “var” in javascript :))
    As the name says, the variable will be implicitly typed to the type of the value
    it’s being assigned. In other words, type of the variable will be inferred from
    the value being assigned to it.
    e.g.
    var i = “hello world”;
    is equivalent to
    string i = “hello world”;

    Well there are some restrictions as well maybe because it’s only a compiler level feature which will be preprocessed and make the variables typed.

    1. As the type of variable is “inferred” from the assignment expression in its declaration,
      the declaration must have an initializer.
      So, var i; is invalid whereas var i = 5; is valid.
    2. the initializer cannot be “Collection Initializer”.
      So, var i = {5, 8}; is not valid whereas var i = new int[]{5, 8}; is valid.
      (we have the element type so i don’t think this should be a restriction.
      if we have
      var i ={“data1”, “data2”); Here the element type is “string” so can’t the compiler infer the var type to be string array i.e. “string[]” ??)
    3. the variable cannot be initialized with null as no type can be inferred from null.
      So, var i = null; is not valid.
      The same can be used for foreach iteration variable as well. The type will be inferred to be the element type of the collection being enumerated through.
  2. Extension methods

    Extension methods are static methods which extend functionality of classes without needing the classes to be changed.
    Extension methods are any regular static methods in static classes (Utility methods) except that they can be invoked using instances as well.

    Defining Extension methods:


    namespace Extensions
    {
        //provides some utility methods
        //for parsing string representations to their corresponding objects
        public static class ParseHelper
        {
            public static Person ToPerson(string stringRepresentation)
            {
            }
            public static Account ToAccount(string stringRepresentation, bool setDefaultValuesForOtherProperties)
            {
            }
        }

        //provides some utility methods using Extension methods
        //for parsing string representations to their corresponding objects
        public static class ParseHelperUsingExtensions
        {
            public static Person ToPerson(this string stringRepresentation)
            {
            }
            public static Account ToAccount(this string stringRepresentation, bool setDefaultValuesForOtherProperties)
            {
            }
        }
    }

    See the difference between the 2 classes defined above.
    The second class (ParseHelperUsingExtensions) has the keyword “this” in its parameter.
    This keyword here defines that ToPerson and ToAccount methods can be invoked on an string instance as well.
    ( as though we just added two methods in System.String class 🙂 )

    Invoking Extension methods:

    The extension methods defined above can be invoked in 2 ways:

    string strVal = “some value”;

    1. the usual way:
      Person person = ParseHelperUsingExtensions.ToPerson(strVal);
      Account account = ParseHelperUsingExtensions.ToAccount(strVal, true);
    2. the new way:
      Person p = strVal.ToPerson();
      Account account = strVal.ToAccount(true);

      This will first check for the matching method defined in String. If not found it will check in the utility class.
      If there was a method ToPerson or ToAccount defined in System.String already, that would have overridden the utility method implementation.

Continued at teqilite.blogspot.com

Advertisements

One Response to “New features in C# 3.0 (“C# Orcas”)”

  1. naisioxerloro Says:

    Hi.
    Good design, who make it?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s