Bee Pee

Surfing through LINQ

Using Implicitly Typed Local Variables (var keyword): Instead of explicitly declaring the type of variable as into, string or decimal the keyword var signals the compiler that the type of variable needs to be determined from the type of the initial value supplied to it. This sounds like the Variant keyword in VB6, however we need to take a note that once the type of a variable is declared you cannot assign it to another type. Thus, .net maintains it type safety.

Using implicitly typed local variable declaration with long and complicated type simplifies the variable declarations, reducing the amount of code required and associated coding error

You cannot use the key word var without providing the intializer i.e. you cannot say var x; or var x= {1, 2, 3} it needs to be
var x=new int[] {1, 2, 3}

Extending Types with Extension Methods (this keyword):
You can actually add your own methods which can be invoked from the instance of the type. This can be done by adding the keyword this to the first argument of the method. These methods however need to be static and public. The dot net documentation says that the new method would appear as in the list of the method, however it does not appear. But I managed to compile the application by typing the name of method directly. It also returned the right result indeed. I guess the method name not appearing in the list can be attributed to the preview version, Microsoft would sort it out with their new realase.We can also apply the same to generic types as well.

Working with Lambda Expressions (=> operator):
With .net 2.0 Microsoft had introduced anonymous functions taking this forward in .net 3.0 Microsoft introduced LAMBDA expression (=> operator) this forms the shortcut. Lambda expression can be applied to simple expression like finding if a number is even or odd for ex i => (i % 2) == 0; or it could have multiple parameters when it has multiple parameters we need to enclose the parameters in parenthesis for ex (x,y) => return x * y;
When you import the namespace System.Expressions a new type is exposed by LINQ, Expression which represents an expression tree, an in-memory representation of a lambda expression. It enables you to represent the lambda expression as expression tree.

Easy Initialization with Object and Collection Initializers:
LINQ also brings along with itself the ease of object initialization you can initialize the object by just providing the value of members in {} bracket. Dot net here uses the parameter less constructor to initialize the values. It is not compulsory to pass values of all the members. Wherever the values are not supplied the members are initialized with default values.
However, you cannot skip a member in between for ex if you have a class Triangle{Point a,b,c} you can initialize the triangle by saying var r1 = new Triangle { p1 = a, p2 = b }; , but you cannot say var r1 = new Rectangle { p1 = a, , p3 = c }; for such case you would require to say var r1 = new Rectangle { p1 = a, p3 = c};and in this case point p2 would be initialized with default values. The same funda applies to the collections as well which implement System.Collections.Generic.ICollection

Using Anonymous Types:
In c#3.0 when we would be able to declare an anonymous type and return the instance of the same, the same can be accomplished by using the new operator.

Understanding Queries and Query Expressions
When using the sql query in the code we cannot ensure the type safety of the parameters passed or the sql query itself would be complied or not is also a question. How ever by using the LINQ query expression. This is enabled by importing the System.Query namespace. Like the sql queries you could also query the classes and go ahead and also perform a join on them.

One thought on “Surfing through LINQ