Nice reference for C# evolution Part 2

Csharp
Csharp

Nice reference for C# evolution Part 2

Welcome to CodeSpread!

We completed till C# 2.0 introductions in last article. We will forward our journey with C# 3.0 features.

C# 3.0

Lambda Expressions:

Last article, we discussed anonymous function and how they extend delegates. Similarly, Lambda expressions extends anonymous methods. All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block. In the hidden form, we are using delegates only and making it more useful by mixing it with LINQ.

It is not a distinct functionality but a short structure of delegates.

Example: We have a list of employees


ListemployeeList= new List();

//Find out employee with id 1

Employee  employee=employeeList.where(x=>x.id==1)

This “x=>x.id==1” is the lambda expression which is used as a short hand instead of writing a full delegate or anonymous method to evaluate the expression.

Extension Methods:

Extension methods are used to extend the existing CLR type like int and string. Extension methods are clean,simple and quickly available in the intellisense.We can avoid any other approaches like sub-class the original type or recompile it.We will perform the following steps.

  • Create a public static class
  • Define static function
  • Use ‘this’ with the type in the parameter which extends the function.

public static class MyExtensions
{
public static string Value(this String str)
{
return str.Append(“Hello”);

}
}

//Call this Extension Method

string Name= “CodeSpread”;

Name.Value(Name);

If we have not used extension methods, we might have to create a function which has to be called again and again to append “Hello” with the string. Now Value() method can directly be called by any string variable.

Expression Tree

Now, we know lambda expression so it will be easier to understand Expression trees.What we achieve by expression tree over lambda expression is that expressions are analyzed at runtime.Expression trees are special kind of binary trees and as binary trees allows us to quickly find the requested data,It is  possible by constructing dynamic linq queries.It will be useful in scenarios like-bind query to get data and sort the data dynamically.

Syntax:
Expression<func> = (param) => lamdaexpression;

Lets see the code.It is a simple declaration of lambda expression that takes two numbers as parameters and returns true whenever first number is less than the second one and false otherwise.


Func<int, int, bool> f = (a, b) => a < b;

It can be written as an Expression


Expression<Func<int, int, bool>> f = (a, b) => a < b;

Note: Expression tree is more of a data structure

Anonymous Types

Refer this article

Implicit typing

Don’t get confused with anonymous type because Implicit typing is the ‘var’ keyword which instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement.


var name = “CodeSpread”;

In this code, variable ‘name’ infers the type as ‘string’. In case of Anonymous Types, type at the right hand side can be only explicit type and not implicit.

LINQ

It is difficult to cover LINQ in few statements so we will see only a higher level view and not go in detail.

  • LINQ is Language Integrated Query.
  • LINQ provides extensibility and gifts powerful query capabilities to the language syntax of C# and Visual Basic.
  • A single syntax to access all kind of data like file-based data, in-memory data, or XML data.
  • Query generated is well structured and very optimal
  • Allows direct translation of business rules.

There are ORMs built on top of LINQ syntax like

  • LINQ to Object
  • LINQ to ADO.Net
    • LINQ to SQL
    • LINQ to DataSet
    • LINQ to Entities
  • LINQ to XML

Example


List<Employee> employeeList= new List<Employee>();

var employee= from emp in employeeList where emp.Id=1;

This Linq query will return the employee having id as 1 from the list of employees.

We will cover .Net 4.0 features in the next article.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • Nice reference for C# evolution Part 3Nice reference for C# evolution Part 3We completed till C# 3.0 introductions in last article and C# 2.0 in this article . We will forward our journey with C# 4.0 features. C#4.0 Late Binding (Dynamic) Late binding is always presumed...
  • Nice reference for C# evolution Part 4Nice reference for C# evolution Part 4We completed till C# 4.0 introductions in last article,C# 3.0 in this article and C# 2.0 in first article . We will forward our journey with C# 5.0 features. C# 5.0 Async Feature There are new m...
  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • Nice reference for C# evolutionNice reference for C# evolutionWhile going through some posts on MSDN, I came across this beautiful image which can be termed as a one stop solution for C# evolution. This is a gift reference, I would like to share with my frien...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • RegisterStartupScript, RegisterOnSubmitStatementRegisterStartupScript, RegisterOnSubmitStatementWe have discussed RegisterClientScriptBlock, RegisterClientScriptInclude in our previous article here. There are two more methods to client script dynamically from server side. We will discuss thes...
  • 20 Dec: Must Read Codes [6-10]20 Dec: Must Read Codes [6-10]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • Web-Page responses by PostBack, AutoPostBack, CrossPostBack, CallBack.Web-Page responses by PostBack, AutoPostBack, CrossPostBack, CallBack.It is difficult to find a site on the internet which does not respond to user's action. By user's action, i mean to say, clicking on a button, selecting something from a list, navigation links. The...