Wednesday, February 24, 2010

What's New in C# 3.0

What's New in C# 3.0

On the heels of the Visual Studio 2005 and C# 2.0 releases, Microsoft has given a sneak preview of what to expect in the version after the next: C# 3.0. Even though C# 3.0 is not even standardized yet, Microsoft provided a preview release at its Professional Developers Conference (PDC) in September so eager developers could try out some of the expected features.

This article discusses the following major new enhancements expected in C# 3.0:

* Implicitly typed local variables
* Anonymous types
* Extension methods
* Object and collection initializers
* Lambda expressions
* Query expressions
* Expression Trees


Implicitly Typed Local Variables

C# 3.0 introduces a new keyword called "var". Var allows you to declare a new variable, whose type is implicitly inferred from the expression used to initialize the variable. In other words, the following is valid syntax in C# 3.0:

var i = 1;

The preceding line initializes the variable i to value 1 and gives it the type of integer. Note that "i" is strongly typed to an integer—it is not an object or a VB6 variant, nor does it carry the overhead of an object or a variant.

To ensure the strongly typed nature of the variable that is declared with the var keyword, C# 3.0 requires that you put the assignment (initializer) on the same line as the declaration (declarator). Also, the initializer has to be an expression, not an object or collection initializer, and it cannot be null. If multiple declarators exist on the same variable, they must all evaluate to the same type at compile time.

Implicitly typed arrays, on the other hand, are possible using a slightly different syntax, as shown below:

var intArr = new[] {1,2,3,4} ;

The above line of code would end up declaring intArr as int[].

The var keyword allows you to refer to instances of anonymous types (described in the next section) and yet the instances are statically typed. So, when you create instances of a class that contain an arbitrary set of data, you don't need to predefine a class to both hold that structure and be able to hold that data in a statically typed variable.

Anonymous Types
C# 3.0 gives you the flexibility to create an instance of a class without having to write code for the class beforehand. So, you now can write code as shown below:

new {hair="black", skin="green", teethCount=64}

The preceding line of code, with the help of the "new" keyword, gives you a new type that has three properties: hair, skin, and teethCount. Behind the scenes, the C# compiler would create a class that looks as follows:

class __Anonymous1
{
private string _hair = "black";
private string _skin = "green";
private int _teeth = 64;
public string hair {get { return _hair; } set { _hair = value; }}
public string skin {get { return _skin; } set { _skin = value; }}
public int teeth {get { return _teeth; } set { _teeth = value; }}
}

In fact, if another anonymous type that specified the same sequence of names and types were created, the compiler would be smart enough to create only a single anonymous type for both instances to use. Also, because the instances are, as you may have guessed, simply instances of the same class, they can be exchanged because the types are really the same.

Now you have a class, but you still need something to hold an instance of the above class. This is where the "var" keyword comes in handy; it lets you hold a statically typed instance of the above instance of the anonymous type. Here is a rather simple and easy use of an anonymous type:

var frankenstein = new {hair="black", skin="green", teethCount=64}


Extension Methods

Extension methods enable you to extend various types with additional static methods. However, they are quite limited and should be used as a last resort—only where instance methods are insufficient.

Extension methods can be declared only in static classes and are identified by the keyword "this" as a modifier on the first parameter of the method. The following is an example of a valid extension method:

public static int ToInt32(this string s)
{
return Convert.ToInt32(s) ;
}

If the static class that contains the above method is imported using the "using" keyword, the ToInt32 method will appear in existing types (albeit in lower precedence to existing instance methods), and you will be able to compile and execute code that looks as follows:

string s = "1";
int i = s.ToInt32();


This allows you to take advantage of the extensible nature of various built-in or defined types and add newer methods to them.

Object and Collection Initializers

C# 3.0 is expected to allow you to include an initializer that specifies the initial values of the members of a newly created object or collection. This enables you to combine declaration and initialization in one step.

For instance, if you defined a CoOrdinate class as follows:

public class CoOrdinate
{
public int x ;
public int y;
}

You then could declare and initialize a CoOrdinate object using an object initializer, like this:

var myCoOrd = new CoOrdinate{ x = 0, y= 0} ;

The above code may have made you raise your eyebrows and ask, "Why not just write the following:"

var myCoOrd = new CoOrdinate(0, 0) ;

Note: I never declared a constructor that accepted two parameters in my class. In fact, initializing the object using an object initializer essentially is equivalent to calling a parameterless (default) constructor of the CoOrdinate object and then assigning the relevant values.

Similarly, you should easily be able to give values to collections in a rather concise and compact manner in C# 3.0. For instance, the following C# 2.0 code:

List animals = new List();
animals.Add("monkey");
animals.Add("donkey");
animals.Add("cow");
animals.Add("dog");
animals.Add("cat");

Now can be shortened to simply:

List animals = new List {
"monkey", "donkey", "cow", "dog", "cat" } ;


C# 1.x allowed you to write code blocks in methods, which you could invoke easily using delegates. Delegates are definitely useful, and they are used throughout the framework, but in many instances you had to declare a method or a class just to use one. Thus, to give you an easier and more concise way of writing code, C# 2.0 allowed you to replace standard calls to delegates with anonymous methods. The following code may have been written in .NET 1.1 or earlier:

class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = new DemoDelegate(SayHi);
myDelegate();
}
void SayHi()
{
Console.Writeline("Hiya!!") ;
}
}

In C# 2.0, using anonymous methods, you could rewrite the code as follows:

class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = delegate()
{
Console.Writeline("Hiya!!");
};
myDelegate();
}
}

Whereas anonymous methods are a step above method-based delegate invocation, lambda expressions allow you to write anonymous methods in a more concise, functional syntax.

You can write a lambda expression as a parameter list, followed by the => token, followed by an expression or statement block. The above code can now be replaced with the following code:

class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = () => Console.WriteLine("Hiya!!") ;
myDelegate();
}
}

Although Lambda expressions may appear to be simply a more concise way of writing anonymous methods, in reality they also are a functional superset of anonymous methods. Specifically, Lambda expressions offer the following additional functionality:

* They permit parameter types to be inferred. Anonymous methods will require you to explicitly state each and every type.
* They can hold either query expressions (described in the following section) or C# statements.
* They can be treated as data using expression trees (described later). This cannot be done using Anonymous methods.

Query Expressions

Even though further enhancements may be introduced in the coming months as C# 3.0 matures, the new features described in the preceding sections make it a lot easier to work with data inside C# in general. This feature, also known as LINQ (Language Integrated Query), allows you to write SQL-like syntax in C#.

For instance, you may have a class that describes your data as follows:

public class CoOrdinate
{
public int x ;
public int y;
}

You now could easily declare the logical equivalent of a database table inside C# as follows:

// Use Object and collection initializers
List coords = ... ;


And now that you have your data as a collection that implements IEnumerable, you easily can query this data as follows:

var filteredCoords =
from c in coords
where x == 1
select (c.x, c.y)

In the SQL-like syntax above, "from", "where", and "select" are query expressions that take advantage of C# 3.0 features such as anonymous types, extension methods, implicit typed local variables, and so forth. This way, you can leverage SQL-like syntax and work with disconnected data easily.

Each query expression is actually translated into a C#-like invocation behind the scenes. For instance, the following:

where x == 1

Translates to this:

coords.where(c => c.x == 1)

As you can see, the above looks an awful lot like a lambda expression and extension method. C# 3.0 has many other query expressions and rules that surround them.

Expression Trees

C# 3.0 includes a new type that allows expressions to be treated as data at runtime. This type, System.Expressions.Expression, is simply an in-memory representation of a lambda expression. The end result is that your code can modify and inspect lambda expressions at runtime.

The following is an example of an expression tree:

Expression filter = () => Console.WriteLine("Hiya!!") ;

With the above expression tree setup, you easily can inspect the contents of the tree by using various properties on the filter variable.
One to Grow On

C# 3.0 offers incredible new features that make your work as an application developer and architect a lot easier, and yet it remains a programming language that lends itself to stricter and cleaner architecture.

C# 3.0 is in its infancy right now and it will mature in the coming months, but given the sizable impact its changes will have on the surrounding .NET Framework, its recommended architecture, and design patterns, definitely keep your eye on it.

www.csharp-station.com/ for other topics

Tuesday, February 23, 2010

JOINs In SQL SERVER










Contents [hide]


Introduction


In this article, we’ll see the basic concepts of SQL JOINs. In the later part of the article, we’ll focus on the advanced subject of Self-JOIN and some interesting observations on how inner JOIN can be simulated using left JOIN. The author has tried his best to amalgamate various topics in a single concept.

The JOIN keyword is used in a SQL statement to query data from two or more tables based on a relationship between certain columns in these tables.


Inner JOIN


A JOIN that displays only rows that have a match in both the JOINed tables is known as inner JOIN.  This is the default type of JOIN in the Query and View Designer.


Outer JOIN


A JOIN that includes rows even if they do not have related rows in the joined table is an Outer JOIN.  You can create three different outer JOINs to specify the unmatched rows to be included:

Left Outer JOIN: In Left Outer JOIN, all rows in the first-named table, i.e. “left” table, which appears leftmost in the JOIN clause, are included. Unmatched rows in the right table do not appear.

Right Outer JOIN: In Right Outer JOIN, all rows in the second-named table, i.e. “right” table, which appears rightmost in the JOIN clause, are included. Unmatched rows in the left table are not included.

Full Outer JOIN: In Full Outer JOIN, all rows in all the joined tables are included, whether they are matched or not.


Additional Notes related to JOIN


The following are three classic examples to demonstrate the cases where Outer JOIN is useful. You must have noticed several instances where developers write query as given below.

The query demonstrated above can be easily replaced by Outer JOIN. Indeed, replacing it by Outer JOIN is the best practice. The query that generates the same result as above is shown here using Outer JOIN and WHERE clause in JOIN.

The above example can also be created using Right Outer JOIN.

NOT Inner JOIN

Remember, the term Not Inner JOIN does not exist in database terminology. However, when full Outer JOIN is used along with WHERE condition, as explained in the above two examples, it will give you exclusive result to Inner JOIN. This JOIN will show all the results that were absent in Inner JOIN.


Cross JOIN


A cross JOIN devoid of a WHERE clause produces the Cartesian product of the tables involved in the JOIN. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. One common example is when a company lists all its products in a pricing table to compare each product with others prices.


Self-JOIN


In this particular case, one table JOINs to itself with one or two aliases to stave off confusion. A self-JOIN can be of any type, as long as the joined tables are the same. A self-JOIN is unique in the sense that it involves a relationship with only one table. A common example is when a company has a hierarchal reporting structure whereby a member of staff reports to another member. Self-JOIN can either be an Outer JOIN or an Inner JOIN.

Self-JOIN is accomplished by using table name aliases to give each instance of the table a separate name. Joining a table to itself can be useful when you want to compare values in a column to other values of the same column. Self-JOIN is a JOIN in which records from a table are combined with other records from the same table when there are matching values in the joined fields. A self-JOIN can either be an inner JOIN or an outer JOIN. A table is joined to itself based upon a field or combination of fields that have duplicate data in different records. The data type of the inter-related columns must be of the same type or cast to the same type.

Now, think of a situation where all the data you require is contained within a single table, but data needed to extract is related to each other in the table itself. Examples of this type of data relate to employee information, where the table may have both an employee’s ID number for each record and also a field that displays the ID number of an employee’s supervisor or manager. To retrieve the data, it is mandatory for the tables to relate/JOIN to itself.

Another example that can be tried on SQL SERVER 2005 sample database AdventureWorks is to find products that are supplied by more than one vendor. Please refer to the sample database for table structure.


Note:

Before we continue further let me make it very clear that INNER JOIN should be used where it cannot be used and simulating INNER JOIN using any other JOINs will degrade the performance. If there are scopes to convert any OUTER JOIN to INNER JOIN, it should be done with priority.


Run the following two scripts and observe the result-set. It will be identical.

After gazing at the identical result the first question that cropped up in my mind was - what is behind the scene plan? Looking at the actual execution plan of the query it is quite evident that even if LEFT JOIN is used in SQL Server Query Optimizer, it converts to INNER JOIN since results are the same and performance is better.

Looking at the above scenario it makes me ponder how smart Query Optimizer Engine is and how it might be saving innumerable performance-related issues for sub-optimal queries.

Now let us try to grasp the cause of LEFT JOIN acting as INNER JOIN. When 1= 1 is used in ON clause it is always true and converts LEFT JOIN to CROSS JOIN. However, when WHERE condition’s effect is applied to the above CROSS JOIN it produces a result similar to INNER JOIN in our case. SQL Server Query Optimizer interprets this in advance and uses INNER JOIN right away.

I think a good question to ask in an interview would be -“How to write an OUTER JOIN that will give you the exact result, execution plan and performance as INNER JOIN?”

If there is any other explanation apart from what I have given or if you want to share a similar example

Thursday, February 18, 2010

.Net certification

When you earn a Microsoft Certified Technology Specialist (MCTS) certification, you validate your in-depth technical knowledge and skill in using the features and functionality of key technology areas in Microsoft Visual Studio and the Microsoft .NET Framework to design software applications.

You can earn a single certification to show your depth of knowledge in a specific Visual Studio technology, earn multiple certifications to demonstrate your breadth of understanding across different specialties, or build on your MCTS certification to earn a Professional Series credential.

Learn about the MCPD credential
Each MCTS certification requires two exams: a core exam and one additional, specialized exam. Use the following table to determine the certification that best fits your current role and career goals.

Note You need to pass Exam 70-536 only once for MCTS or MCPD certification.

Individual MCTS certifications will be retired when Microsoft discontinues mainstream support for the related technology.

Technology
Usage
Certification
Exam

Visual Studio 2005
Developing Windows-based applications that include Windows Forms technology and data access
MCTS: .NET Framework 2.0: Windows Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-526: TS: Microsoft .NET Framework 2.0 - Windows-Based Client Development

Visual Studio 2005
Developing Web-based applications that run on ASP.NET and the .NET Framework 2.0
MCTS: .NET Framework 2.0: Web Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70–528: TS: Microsoft .NET Framework 2.0 - Web-based Client Development

Visual Studio 2005
Developing distributed applications that demonstrate a knowledge of Web services, .NET remoting, Enterprise Services, and Message Queuing technology
MCTS: .NET Framework 2.0: Distributed Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-529: TS: Microsoft .NET Framework 2.0 - Distributed Application Development

Visual Studio 2008
Creating applications that run on the Windows platform and compelling user interfaces with Windows Presentation Foundation
MCTS: .NET Framework 3.5, Windows Presentation Foundation Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-502: TS: Microsoft .NET Framework 3.5, Windows Presentation Foundation Application Development

Visual Studio 2008
Creating distributed applications that communicate with servers or other applications in a connected or disconnected state
MCTS: .NET Framework 3.5, Windows Communication Foundation Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-503: TS: Microsoft .NET Framework 3.5 – Windows Communication Foundation Application Development

Visual Studio 2008
Creating applications that host workflows for your organization
MCTS: .NET Framework 3.5, Windows Workflow Foundation Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-504: TS: Microsoft .NET Framework 3.5 – Windows Workflow Foundation Application Development

Visual Studio 2008
Creating Windows-based applications that run on corporate servers or user desktop computers
MCTS: .NET Framework 3.5, Windows Forms Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-505: TS: Microsoft .NET Framework 3.5, Windows Forms Application Development

Visual Studio 2008
Creating data-driven applications that access data from various sources, such as SQL Server, Oracle, Microsoft Office Access, object data sources, XML, or other flat-file sources
MCTS: .NET Framework 3.5, ADO.NET Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-561: TS: Microsoft .NET Framework 3.5, ADO.NET Application Development

Visual Studio 2008
Creating Web-based applications that run on the ASP.NET platform and are hosted on Internet Information Server
MCTS: .NET Framework 3.5, ASP.NET Applications
Exam 70-536: TS: Microsoft .NET Framework – Application Development Foundation

and

Exam 70-562: TS: Microsoft .NET Framework 3.5, ASP.NET Application Development



When you earn the Microsoft Certified Professional Developer (MCPD) credential, you demonstrate your expertise in using Microsoft Visual Studio and the Microsoft .NET Framework to excel in a specific, market-relevant job role.

We offer three professional certification paths that build on the MCTS certifications.

Read about MCTS certifications
Learn more about the MCPD credential
Technology
Certification
Prerequisites
Exam

Visual Studio 2005
MCPD: Web Developer
MCTS: .NET Framework 2.0: Web Applications (Exam 70-536 and Exam 70-528)
Exam 70-547: PRO: Designing and Developing Web-Based Applications by Using the Microsoft .NET Framework

Visual Studio 2005
MCPD: Windows Developer
MCTS: .NET Framework 2.0: Windows Applications (Exam 70-536 and Exam 70-526)
Exam 70-548: PRO: Designing and Developing Windows-Based Applications by Using the Microsoft .NET Framework

Visual Studio 2005
MCPD: Enterprise Application Developer
MCTS: .NET Framework 2.0: Windows Applications (Exam 70-536 and Exam 70-526)

MCTS: .NET Framework 2.0: Web Applications (Exam 70-536 and Exam 70-528)

MCTS: .NET Framework 2.0: Distributed Applications (Exam 70-536 and Exam 70-529)
Exam 70-549: PRO: Designing and Developing Enterprise Applications by Using the Microsoft .NET Framework

Visual Studio 2008
MCPD: Windows Developer 3.5
MCTS: .NET Framework 3.5, Windows Forms Applications (Exam 70-536 and Exam 70-505)
Exam 70-563: PRO: Designing and Developing Windows Applications Using Microsoft .NET Framework 3.5

Visual Studio 2008
MCPD: ASP.NET Developer 3.5
MCTS: .NET Framework 3.5, ASP.NET Applications (Exam 70-536 and Exam 70-562)
Exam 70-564: PRO: Designing and Developing ASP.NET Applications Using Microsoft .NET Framework 3.5

Visual Studio 2008
MCPD: Enterprise Application Developer 3.5
MCTS: .NET Framework 3.5, Windows Forms Applications (Exam 70-536 and Exam 70-505)

MCTS: .NET Framework 3.5, ASP.NET Applications (Exam 70-536 and Exam 70-562)

MCTS: .NET Framework 3.5, ADO.NET Applications (Exam 70-536 and Exam 70-561)

MCTS: .NET Framework 3.5, Windows Communication Foundation Applications (Exam 70-536 and Exam 70-503)
Exam 70-565: PRO: Designing and Developing Enterprise Applications Using the Microsoft .NET Framework 3.5



Upgrade from MCAD or MCSD to MCTS or MCPD on Visual Studio 2005
Although Microsoft Certified Application Developer (MCAD) and Microsoft Certified Solutions Developer (MCSD) credentials continue to be recognized and valued by employers and decision makers who use Microsoft .NET 1.0 and Microsoft .NET 1.1, you can now easily upgrade your existing MCAD or MCSD credential to Microsoft Certified Technology Specialist (MCTS) or Microsoft Certified Professional Developer (MCPD) on Visual Studio 2005. Depending on your current certification, you will need to take one or two exams.

Once you have earned the MCPD on Visual Studio 2005, you can upgrade to MCPD on Visual Studio 2008.

Current certification
Certification goals
Exams

MCAD
MCTS: .NET Framework 2.0 Windows Applications
Exam 70-558: Upgrade: MCAD Skills to MCTS Windows Applications by Using the Microsoft .NET Framework

MCAD
MCTS: .NET Framework 2.0 Web Applications
Exam 70-559: Upgrade: MCAD Skills to MCTS Web Applications by Using the Microsoft .NET Framework

MCAD
MCTS: .NET Framework 2.0 Web Applications and MCPD: Web Developer, Visual Studio 2005
Exam 70-551: Upgrade: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework

MCAD
MCTS: .NET Framework 2.0 Windows Applications and MCPD: Windows Developer, Visual Studio 2005
Exam 70-552: Upgrade: MCAD Skills to MCPD Windows Developer by Using the Microsoft .NET Framework

MCAD
MCTS: .NET Framework 2.0 Distributed Applications

MCPD: Enterprise Applications Developer on Visual Studio 2005 certification

Note You must have already passed Exam 70-551 and Exam 70-522.
Exam 70-529: TS: Microsoft .NET Framework 2.0 – Distributed Application Development

and

Exam 70-549: PRO: Designing and Developing Enterprise Applications by Using the Microsoft .NET Framework

MCSD
MCTS: .NET Framework 2.0 Web Applications

MCTS: .NET Framework 2.0 Windows Applications

MCTS: .NET Framework 2.0 Distributed Applications

MCPD: Enterprise Applications Developer on Visual Studio 2005
Exam 70-553: Upgrade: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 1

and

Exam 70-554: Upgrade: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 2

MCSD
After you earn the MCPD: Web Developer and MCPD: Windows Developer certifications, you can earn the MCPD: Enterprise Applications Developer certification with one exam.
Exam 70-554: Upgrade: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 2


Upgrade from MCPD on Visual Studio 2005 to MCPD on Visual Studio 2008
If you have already earned the MCPD credential on Visual Studio 2005, you can follow a simple upgrade path to earn the MCPD on Visual Studio 2008.

Current certification
Certification goals
Exam

MCPD: Windows Developer (Visual Studio 2005)
MCPD: Windows Developer 3.5

MCTS: .NET Framework 3.5, Windows Applications
Exam 70-566: Upgrade: Transition Your MCPD Windows Developer Skills to MCPD Windows Developer 3

MCPD: Web Developer (Visual Studio 2005)
MCPD: ASP.NET Developer 3.5

MCTS: .NET Framework 3.5, ASP.NET Applications
Exam 70-567: Upgrade: Transition your MCPD Web Developer Skills to MCPD ASP.NET Developer 3.5

MCPD: Enterprise Application Developer (Visual Studio 2005)
MCPD: Enterprise Application Development 3.5

MCTS: .NET Framework 3.5, Windows Forms Applications

MCTS: .NET Framework 3.5, ASP.NET Applications

MCTS: .NET Framework 3.5, Windows Communication Foundation Applications

MCTS: .NET Framework 3.5, ADO.NET Applications
Exam 70-568: Upgrade: Transition your MCPD Enterprise Application Developer Skills to MCPD Enterprise Applications Developer 3.5, Part 1

and

Exam 70-569: Upgrade: Transition your MCPD Enterprise Application Developer Skills to MCPD Enterprise Applications Developer 3.5, Part 2

Wednesday, February 17, 2010

Windows server 2008 extend period on expiring

To enjoy the free activation period in Windows Server 2008, users need to install Windows Server 2008 without activating it. To do so, run the Windows Server 2008 Setup program, but do no enter any product key when prompted to enter a product key for activation. Then, click “No” when asked to confirm your selection. The initial 60-day evaluation period which do not require activation be done starts after installation finished.

How to manually extend the activation grade period on expiring

When the 60-day activation grace period is about to expire and end, run the slmgr.vbs script to reset and extend the evaluation period for another new 60-day trial evaluation period, with everything including programs installed and data intact.

Open an elevated privilege command prompt.
Type the slmgr.vbs -rearm, and then press Enter to reset the activation grace period to 60 days.
Restart the computer.
To check the status or time that is left on current activation grace period or evaluation period, run slmgr.vbs -dli command to display the number of days that are left in the current 60-day evaluation period.

Popular Posts

Recent Posts

Unordered List

Text Widget