Explaining C# Features in LINQ

21. July 2009

 

I will go into some of the newer features of C# 3.0 specifically those that are involved with LINQ.

On July 25th and 26th the BSDA will be hosting an event called LINQ Bootcamp where presenters will go through the book “Programming Microsoft LINQ” chapter by chapter (1 – 12).  It will be a fun weekend and a great chance to get your feet wet with LINQ.

If you don’t think you will be able to make it to the event I would still check out the book.

book-300x300

With that said let's dive right in!

Implicitly Typed Variables

These are just variables declared with the var keyword and assigned a value. Once assigned a value and the type is inferred based on the value it is set to. Once set the type of the variable cannot be changed. Basically the var keyword is another way for you to be lazy - The compiler will do all the work for you to determine what the type of that variable should be and then set it. Once it is set you cannot set it to another type.

 

Example:

            
    var myVariable = 1;
    myVariable = "Test"; // this will not work because you cannot convert (implicitly) a string to an int.

 

Object Initializers

This is another shorthand, this is a way of instantiating an object and assign values to it's properties in one step, sort of like is possible with Built-In data types (int, string, bool...)

Example:

            int indexer = 1;
            string name = "test";
            bool isActive = true;

However, before the appearence of object initializers in C# to create an instance and initialize a custom business object you would need to do something like the following (unless you created several overloaded constructors):

            Person person = new Person();
            person.Age = 32;
            person.FirstName = "John";
            person.LastName = "Doe";

 

Using this shortcut you can now initialize a custom object as follows:

            Person person = new Person
            {
                Age = 32,
                FirstName = "John",
                LastName = "Doe"
            };

 

Anonymous Types

Anonymous types are a shortcut way to create custom objects with read-only properties. The compiler will generate a type name for the anonymous type and infer the properties for that type. Since the object type is not available before compile time the use of Implicitly typed variables and Object Initializers are used here.

Example:

            var person = new 
            { 
                Age = 32, 
                FirstName = "John", 
                LastName = "Doe" 
            };

            person.Age = 53; // Cannot do this - Read-Only Property.

 

Extension Methods

Basically an extension method is a shorthand way of creating a wrapper method. When compiled the extension method is actually called as if it was a wrapper method. Extension methods musts be defined as static, their first parameter must be declared with the this modifier and defines the object type that the method applies to.

Example:

namespace LinqBootcamp
{
    public static class IntExtensions
    {
        public static int Add(this int source, int toAdd)
        {
            return source + toAdd;
        }
    }
}

 

Once you have the extension method - you will need to import the namespace containing your extension and then you can use it as follows. In this case  the “Add” method only applies to integers because the first parameter  in the extension method is an integer.

            int test = 5;
            int a, b;

            a = test.Add(5); // a Equals 10
            b = (10).Add(10); // b Equals 20

LINQ , ,

Comments

7/22/2009 11:02:16 PM #
finally i find something that i want to know..
thanks for this usefull informations..
Comments are closed