EventHandler simplified with extension method

Hi,

One of the good uses of extension method that one of my friends told me when in discussion was to also reduce the code, by using extension method in the right place.

The first example we came into was firing of an event. When we need to fire an event we always need to make a check if the Event is null or not. Only in case when event is null (meaning there is at least one function pointer attached) we raise the event.

But this checking for null can be easily made generic with the help of an extension method. We can easily create an extension method on the class EventHandler. After that all we need to do is calling the new extension to raise the event and that will take care of checking for the null. Here is the code for the same

public static void RaiseEvent<TEventArguments>(

Continued...

What happen when we create an extension method with the name with which an instance method already exists

Hi,

One of the interesting things that I have not yet mentioned about the Extension method is there naming convention. In dot net framework 3.5 there is not restriction on the naming convention of the extension methods.

What this means is that we can have two extension methods with the same name. In fact we can have an extension method with the same name as an instance method of the class.

So an extension method like ToString() can be added to the object although the same method already exists in the same class. The method call will be selected in the following order.

Instance method.

Extension method with the same namespace

Extension method outside the current namespace

Vikram


Lambda expression – predicate and projection

Hi,

I have discussed about the lambda expression in my previous posts. In this post I will talk about predicate and projection. Predicate and projection are type of Lambda.

Some of the Lambda expression have particular name, which is based on their purpose. Predicate and projection are two type of Lambda expression, which have their name, based on the purpose.

Predicate – A Boolean expression that is intended to indicate membership of an element in a group is called predicate. Below is an example of the Lambda expression, which is predicate.

( Count ) => count > 1000

Here this lambda expression is returning a Boolean expression and is intended to indicate the membership existence. Hence it is a predicate.

Continued...

Dot net framework 3.5 breaks the LINQ to SQL code. Here are the changes

Hi,

With the release of dot net framework 3.5, its time to get work with LINQ in live projects. For those who have used LINQ in the beta 2 version can use the previous code in the latest version.

But there are small breaking changes that have been made in the framework, which needs to be incorporated in the code of the beta version. Here are some of the changes that need to be made from the previous version

In the LINQ to SQL the Add method has been changed to InsertOnSubmit. Similarly AddAll method name has been changed to InsertAllOnSubmit. The remove and RemoveAll method have also been changed to DeleteOnSubmit and DeleteAllOnSubmit

Another breaking change is that the

<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

[Note: The version has been changed

Continued...

Visual Studio 2008 Orcas changing the default browser and its default size

Hi,

Every one has a different choice when it comes to browser. I personally like Firefox for surfing the net. I should say the number of addin’s available for Firefox are really very good and makes surfing the net and debugging (Javascript) very easy.

When we use Visual Studio and try to debug a Web Application, by default, it uses the IE. Many a times we do not want it use IE but some other browser (Like Firefox).

With Visual Studio 2008 this can be done very easily. To Change the default browser while using View in browser option just right click on the aspx and select “Browse with” context menu. In the next dialogue we can select or also add the browser there. We can also set the default browser for the Visual Studio in there.

Continued...

String class reverse function and extension method

Hi,

One of the most basic functions that I would like to add with the help of the new extension method in dot net 3.5 is the reverse method in the string class. Many a time we want to reverse the character in the string. Currently we do not have any inbuilt method in the framework to reverse the character in a string.

Yes till now we had to work with a static method to reverse the string, but I would love to have this functionality in the string class itself. Here is the implementation of the same.

public static void Reverse(this string strObj)

{

    char[] characters = strObj.ToCharArray();

    Array.Reverse(characters);

    strObj = new string(characters);

}

Continued...

Windows Server 2008 and IIS 7.0 RCO available for download

Hi,

As the title of the post says the season of the RCO has started at the world of Microsoft.

Continued...

Using the Take and Skip Keyword to reduce the number of records fetched in the Query

Hi,

One of the nicest things LINQ makes easy is paging. LINQ seems to de basic paging query out of the box. Paging is well built in LINQ and is very very easy to be used.

With LINQ we no longer have to right those complex logic just to create paging effect. The two mainly keyword mainly used for paging in LINQ framework are Take and Skip.

The Take keyword is used to decide how many records are to be fetched. A simple example of the Take keyword is provided below.

List<Customer> customers = GetCustomerList();
 
    var first3Customers = (
                from c in customers

Continued...

Some more Lambda expression

Hi,

Continuing with my work on different Lambda expression. Here are some more Lambda’s that I wrote (when I was practicing them).

You can get my previous example here
http://www.vikramlakhotia.com/Starting_with_the_basic_of_Lambda_Expression.aspx
http://www.vikramlakhotia.com/Some_examples_on_how_to_use_Lambda_Expression.aspx
http://www.vikramlakhotia.com/Using_Lambda_expressions_in_LINQ.aspx

To get the first record matching certain condition in the list

string strVal = SomesTringCollection.First(str => str[0] == 'V');

As you would have guessed to get the last record we have a Last method

Continued...

Working with LINQ and Lambda expressions

Hi,

Continuing on my post on Lambda and LINQ, in this post I will talk about how to use the lambda expression in the LINQ query. LINQ query can work great without the Lambda expression. But we can also use Lambda expression in the LINQ query.

Continued...

Lambda Expression Usages - some basic examples

Hi,

Continuing from my previous post in this post I will put up some examples of the places and the way we can use Lambda Expression. I will post all the examples based on the where clause of the LINQ query on arbitrary collection of integer, string.

static int[] numbers = new [] { 0,1,2,3,4,5,6,7,8,9 };

static string[] strings = new [] { "a","b","c","d","e","f","g" };

class Employee
{

      public string Name {get; set;}

      public int Salary {get; set;}

}


To filter out only those values which matches a certain condition


var Vnums = numbers.Where(int n => n > 4);

To filter out only those values from the collection of string which matches a certain condition

Continued...

Lambda expression – How to start with the basic of Lambda expression

Hi,

One of the new features of C Sharp 3.0 is Lambda expression. Lambdas are great new thing from language prospective. But for a lower or middle level developer they are not that simple. Actually a common Dot net, C Sharp, developer is not used to the kind of the expression. 

For a normal developer Lambda expressions will be a bit difficult in the beginning. But believe me when you have had a little bit of understanding on how to start with Lambda then You will understand the power of it,

In this post I will show on writing some of the Lambda expression. I hope these examples helps on understanding the Lambda Expression. Let me start with how a Lambda expression is made.,

A Lambda is expression is created by providing the parameter and then the => keyword and then the expression. ,

Continued...

Writing Inner join in LINQ queries

Hi,

I have been playing with LINQ from quite a few days I should say I am impressed with it. It does nearly everything out of box. Yes nearly every thing out of box. And the ORM is also very very powerful. In This post I will illustrate how to use Joins in LINQ queries.

Many a times we want a query where by we retrieve the data from one table and some the related data from the other table. Let says we have a category table and a posts table. Now when I retrieve all the records of the posts I also want to have the related category name (which is there in the category table). So I need to make a join between three tables to get the records. Here is the LINQ Query to do the job.

var t = from p in BlogBLL.Posts

            join cp in BlogBLL.CategoryPosts on p.PostId equals cp.PostId

Continued...

New Lambda Expressions in C Sharp 3.0

Hi,

One of the new features of C Sharp 3.0 is the Lambda expression. As we all know C Sharp 2.0 introduced the concept of anonymous method. An anonymous method allows code methods to be written inline instead of delegate value. A lambda expression provides a more concise way for writing anonymous methods for functional syntax.

Lambda expressions are very useful when used with (in) LINQ queries. A lambda expression provides a type safe and compact way to write which can be passed as argument for subsequent evaluation.

The syntax to write a lambda expression is very simple. The syntax of a lambda expression would be parameter list followed by => token and then the expression block that needs to be executed when the expression is invoked.

Parameter => expression

Continued...

Getting all the methods in a type using LINQ

Hi

One of the most beautiful thing about the new LINQ queries in the Dot net 3.5 is the simplicity with which it lets you query any collection. Yesterday I was playing with the LINQ queries and suddenly a requirement came to me where by I wanted to display all the methods that string (Or any other class you want to choose) class has.

I know this can be done through reflection very easily. But the good thing about LINQ is the simplicity and small amount of code with which I could get the Job done.

All I had to do was declare an object of type “Type”. Assign the object the class I want to use. And use a very simple LINQ query to get all the methods in a collection. Here is the code.


Type tStr = typeof(string);
            var ms = from method in tStr.GetMethods()

Continued...

LINQ the new way to query How to make basic queries

Hi,

One of the best new things in the new Dot net 3.5 is the LINQ. LNQ stands for Language Integrated Query. With the help of LINQ now we can query nearly anything in a more than efficient manner. It also gives us compile time check to the inline query’s, which reduces the chances of runtime error.

A simple query of the LINQ start with the “from” clause and ends with the select clause. This is quite opposite of what we are used to in the SQL (where we have the select clause first and from clause later). Below is the simple example of a Linq Query.

var t = from c in BlogBLL.Categories

               select c;

Continued...

Dot net 3.5 Extension method and its usability

Hi,

Now when I have started working on dot net 3.5 the feature I love most is the extension method. I know many of you will be surprised that it’s not LINQ or the other new feature that are there in visual studio but this one. Well let me tell you how it let me code the way I want to (without leaving me to the limitation of the C sharp).

The kind of coder I am I use the Response.write() a lot. Whenever debugging some problem, for each response I have to add this line. But with extension methods I can add a new extension method to the object class to get this done pretty easily (and make my code look better).

Here is a simple extension method I created to help me out.

public static void Response(this object obj)

{
       Response.write (obj);
}

Continued...

How to install Visual Studio 2008 Beta 2 from img File
Hi
The download of Visual Studio 2008 Beta 2 is provided in a image file. For a novice it might be difficult to use the image file to install the visual studio 2008 beta 2. So I decided to post on the installation of the Visual studio 2008 beta 2. 
You can find the download of Visual Studio 2008 beta 2 here. 
Continued...
            

Download the Orcas Beta 2 with go live licence

Hi,

Here is the news that I have been waiting for so long. The download of Orcas beta 2 is available now. The download comes with a go live licence also. The installation has some problem with the working of asp.net Ajax  side by side. Hence we also have to run a script(provided with the download) at the end of the download. The download supports only Windows XP, Windows Vista and Windows 2003 Server.

Here is the Link To the download
http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx

That’s all for now. I have already started my download manager. What about you?

Vikram

 
Copyright © 2006 - 2008 Vikram Lakhotia