Vakul's .NET Realm

My journey towards .NET World…

  • Details

    Vakuls MCPD Certification Vakuls MCTS Certification Author's Pic



Cool Features of Visual Studio 2008 – Part 2

Posted by Vakul Kumar More on July 12, 2008


It took almost a month and half for me to present Part-2. In second part, I would introduce LINQ, VB 9 and C# 3.0.

Part-2 of this series covers the following topics:

  • LINQ – Language Integrated Query
  • VB.NET: Implicit types
  • VB.NET: Extension Methods
  • VB.NET: IntelliSense
  • C# 3.0: Implicit typed local variables
  • C# 3.0 Object Initializers
  • C# 3.0 : Collection Initializers
  • C# 3.0: Anonymous Types
  • C# 3.0: Lambda Expression

LINQ – Language Integrated Query

LINQ is a new feature in VS 2008 that broadens great querying capabilities into the language syntax. LINQ introduces patterns for querying and updating data. A set of new assemblies are provided that enable the use of LINQ with collections, SQL databases, and XML documents.

Any developer working in Visual Studio 2005 will use stored procedure, view and filtering through ADO.NET for querying a dataset. To facilitate queries inside the languages making developers life easier, emerged LINQ.

To start with, LINQ, a feature of both Visual Basic and C#, uses the concept of the SQL Select statement to make its queries. There are 2 important differences between LINQ and SQL Select statement.

· LINQ statements begin with a From statement instead of the Select statement.

· Since C# and Visual Basic are object-oriented languages, queries written in these languages can encompass anything that is IEnumerable — entities, collections and even XML. Since the queries are written in an object-oriented environment, complex queries can be written for a better result set.

LINQ also brings about the introduction of several new language concepts for Visual Basic and C#. The expression tree, for example, is a data representation of the LINQ expression that bridges the gap between the .NET code and the SQL Server. In addition, property initialization makes it possible to create an object and set its properties in a single line of code.

VB.NET: Implicit types

Implicit types provide strong typing without forcing developers to figure out the type since the type is inferred from the initializer expression.

Inferring types makes it possible for extensions to bind to data types such as XML. This is fundamental to making LINQ work.

VB.NET: Extension Methods

All LINQ query operators are extension methods. These methods are marked with custom attributes and are then added to other objects automatically.

Extension methods cannot contain optional parameters, parameter arrays or generic arguments that have not been typed. Also, late binding cannot be done with extension methods.

Extension methods are shared methods marked with custom attributes that allow them to be invoked with instance-method syntax. Most extension methods have similar signatures.

VB.NET: IntelliSense:

IntelliSense, is used to encompass keywords, member variables and anything in scope. IntelliSense will work with implicit types and is set up to take advantage of IntelliSense. In SQL syntax, the Select query comes first, but in LINQ, the From statement comes first. With the data source listed first, IntelliSense has a chance to kick in.

C# 3.0: Implicit typed local variables:
Local variables can be declared as type var, whose actual type of the variable is determined by the compiler based on the data schema. It’s mainly used to store anonymous types in LINQ.

        // This is an integer

        var nId = 1234579;

        //This is a string

        var strFullname = “Vakul Kumar More”;

C# 3.0: Object Initializers

When you create an instance of the business entity Customer, you write the code to call the class constructor and set the properties of the instance (Listing 3).

Customer objCustomer = new Customer();

objCustomer.Fullname = “Vakul”;

objCustomer.ContactAddress = “My address”;
Object Initializers allow you to re-write the previous code in a concise way

Customer objCustomer = new Customer

{

Fullname=”Vakul”, ContactAddress=”My address”

};  

C# 3.0 : Collection Initializers

Object Initializers is a great feature to write code efficiently. C# also adds a feature to initialize collections of objects called Collection Initializers following the same concepts of Object Initializers.

List<Customer> objCustomerList = new List<Customer>

            {

                new Customer{Fullname=”Vakul Kumar More”, ContactAddress=”Address1″},

                new Customer{Fullname=”Prakash Kyadas”, ContactAddress=”Address2″},

                new Customer{Fullname=”Sateesh Pogula”, ContactAddress=”Address3″}

            };

C# 3.0: Anonymous Types

Anonymous Type is a way to encapsulate properties of an object without defining the underlying class at runtime. The type name and properties are inferred by the compiler.

var objProduct = new { ProductID=1, Name=”My Product”, UnitPrice=10.2};

System.Console.WriteLine(“The product info is: Id={0}, Name={1}, UnitPrice={2}”, objProduct.ProductID, objProduct.Name, objProduct.UnitPrice);

One common scenario where we can use Anonymous Type is when we want to query and work with data using LINQ expressions, and we want to define the schema of the result set in-line.

C# 3.0: Lambda Expression

Lambda expression is an evolution of delegates in order to write in-line anonymous blocks in C#. It’s very helpful when writing Linq query expressions by defining wrapping methods that can be passed as arguments to the evaluations. As illustrative purposes, we’re going to query the list of customer (objCustomerList) defined before. This collection has new extension methods such as Where and Average which can be used along with Lambda expressions to query the array of entities.
Let’s suppose that we want to retrieve information for a particular customer; then we write the C# code along with Lambda expression as a filter as shown.

IEnumerable<Customer> rsCustomer = objCustomerList.Where(p => p.Fullname == “Vakul Kumar More”);

Helpful Links:

  1. LINQ Learning Guide
  2. Visual Studio 2008 Learning Guide
  3. Hidden Undocumented Feature of Visual Studio 2008 – Dynamic XSLT Intellisense
  4. XAML Learning Guide
  5. http://www.theserverside.net/ 
  6. http://weblogs.asp.net/scottgu (Very informative)
Advertisements

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

 
%d bloggers like this: