Thursday, May 31, 2012

What is the query processing order at SQL Server?

Does SQL Server execute the query from top to bottom, starting with the SELECT clause and working its way down? You might think that, but that is not how a query is processed in SQL Server at all. SQL Server logically processes a query in the following order:


(8) SELECT
(9) TOP
(1) FROM
(3) JOIN
(2) ON
(4) WHERE
(5) GROUP BY
(6) WITH
(7) HAVING
(10) ORDER BY

:-)




I collect this information from book Wrox Professional LINQ

How to write an extension method?

Introduction:-

In C# 1.0 - the methods were belonging to a class. That means the methods were defined within the body of the class. C# v2.0 introducing partial classes - the methods making up a class could be defined in more than one place. At compile time they are all collected together. Extension methods are new to C# v3. Extension methods are a powerful new language feature that ensures the code abstraction and increase the code re usability.


How to define extension method?


as per msdn

1. Define a static class with appropriate access modifier.
2. Implement the extension method as a static method with at least same visibility of the containing class.
3. The first parameter of the method specifies the type that the method operates on. It must be preceded with the 'this' modifier.


How to call the extension method?


as per msdn

1. In the calling code, add a using directive to specify the namespace that contains the extension method class.
2. Call the method as if they were instance methods on the type. You do not need to specify the first parameter because it represents the type on which the operator is being applied, and the compiler already knows the type of your object. You only have to provide arguments for parameters 2 through n.


So, extension methods are defined as static methods but are called by using instance method syntax. There first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with using directive.



Example


At first create a new website at Visual Studio 2010.





at New Web Site popup screen

1. Select Visual C# from left side - in which language you want to write code
2. Select ASP.NET Empty Web Site - we will add pages later
3. Then select file location. I rename the website name Website1 to ExamineExtensionMethod.
Then Click Ok.




After clicking Ok the file structure in Solution Explorer tab will be shown like below.





Then add a Class file named MyExtensions and a Web Form





At MyExtensions class remove all code and write the following code-


using System;


namespace CustomExtension

{


public static class MyExtensions
{


public static int WordCount(this string str)
{
return str.Split(new char[] { ' ', '-', '_', ',' }, StringSplitOptions.RemoveEmptyEntries).Length;
}


}

}


Here the important things are namespace, public static class and public static method declaration.





Then at aspx file add a label control.





Then code behind page file

1. add the namespace first
2. then calculate word count of a string.





Thats all.


You need to remember 2 things if you implement extension methods-

1. If you have an extension method and the class(in which your extension method is using) itself implements a method with same name and signature, your extension method will never call.
2. Extension methods are brought into scope at the namespace level.




I would like to name one - Jonathan Worthington from whom I encouraged to write this blog for beginners.