|
Demonstrating Implicit Variable Declaration with C# 3.0
Declaring Local Variables Implicitly
Most statically typed languages in common use today require that when declaring a variable, a data type is specified. For example:int i=10; string s="C Sharp";
C# 3.0 (C# Orcas) or VB 9.0 (VB Orcas) add the interesting behavior of Declaring Local Variables Implicitly. This means that there is no need to mention the data type when declaring a variable.
A local variable can be declared implicitly using the var keyword in C# or the Dim keyword in VB.
| C# 3.0 variable declaration | VB 9.0 variable declaration |
| var i = 10; var s = "C Sharp"; | Dim i = 10 Dim s = "C Sharp |
The above mentioned C# 3.0 and VB 9.0 syntax will be equivalent to the below mentioned C# 2.0 and VB 8.0 or VB 2005 syntax
| C# 2.0 variable declaration | VB 8.0 or VB 2005 variable declaration |
| int i = 10; string s = "C Sharp"; | Dim I As Integer= 10 Dim s As String = "C Sharp" |
Declaring local variables implicitly has some restrictions; the variable must be initialized to some expression that can not be null. Note that the language is still statically typed - the compiler works out the type and "inserts it".
Demonstration
This code example will demonstrate "How to build a LINQ Project", showing use of implicit variable declaration.First, let's see if it is possible to declare variable implicitly with Visual Studio 2005 Project.
1. Open a New, Visual Studio 2005 Project in C#

Figure 1 Create New Visual Studio Project in C#
2. Now Open the Code window and try the code as shown in the figure below:

Figure 2 Trying Implicit DataTypes with Visual Studio 2005 Project in C#
3. Once you Build the solution, you will notice that Visual Studio 2005 does not recognize or "could not find" the variable declared in the code (shown in the code editor in the image above).
4. Since this is a LINQ only feature then, you can only use this with LINQ Projects. You will need to download and install the LINQ Community Technology Preview to try this.
http://www.microsoft.com/downloads/details.aspx?familyid=1e902c21-340c-4d13-9f04-70eb5e3dceea&displaylang=en
Now let's create a LINQ Project in C#:

Figure 3 Create New LINQ project
5. Now open the Code window and try the code as shown in the figure below:

Figure 4 Trying Implicit DataTypes with LINQ Project in C#
6. Once you have typed the code correctly and built the LINQ solution, you will notice that your solution has been built successfully.
Implicitly declared local variables are just one of a number of exciting features coming with C# 3.0 and VB 9.0.
About The Author
Vidya Vrat Agarwal a Microsoft .NET Purist and an MCT, MCSD.NET, MCAD.NET and MCSD. He is also a Life Member of Computer Society of India (CSI). He started working on Microsoft.NET with its beta release. He has been involved in software development, consultation, corporate trainings, T3 programs and technical writing for Microsoft .NET for various corporate clients. He lives with his beloved wife and lovely daughter Vamika (nick named Pearly). He believes that nothing will turn into a reality without them. In technologies the part which really fascinates him is the architecture of that particular technology. He is the follower of the concept No Pain, No Gain and believes that his wife is his greatest strength.
Presently he is working with LioNBridge Technologies in Mumbai - India, where he works with Microsoft Dept. on Visual Studio 2005 (Whidbey) and SQL Server 2005 (Yukon) and his business card reads Subject Matter Expert (SME).
His technical profile holds Microsoft .NET Framework, VC#, VB.NET, Web Services, Remoting, COM, DCOM, COM+, MTS, MSMQ, Visual Studio 2005 (Whidbey), SQL Server 2005 (Yukon) and ORCAS. Besides writing articles for ProgrammersHeaven he also does tech review for Apress Inc.USA for books on .NET and SQL Server 2005. His blog can be read at http://dotnetpassion.blogspot.com
|
|
Janaka Prasad
From Sri Lanka (Report as abusive) |
Mr Thanks for the Article, this works well for newly created DLinq Projects, When I tried to Add DLinq to My Existing Web Application, it doesn't Recognize 'var', and any DLinq Commands.. I Added the all the listed references.. Please Help Me. |
|
Anjum Rizwi
(Report as abusive) |
Good Info Very good info for beginner |
| View all Rate and comment this article |
Sponsored links
Six Sigma Certification
100% Online-Six Sigma Certificate from Villanova - Find Out More Now.
100% Online-Six Sigma Certificate from Villanova - Find Out More Now.
ASP.NET 3.5 Hosting on Windows 2008!
ASP.NET 3.5/2.0 Hosting on Windows 2008 & 2003! AJAX, LINQ, & Silverlight Ready! 3 Mo. Free!!
ASP.NET 3.5/2.0 Hosting on Windows 2008 & 2003! AJAX, LINQ, & Silverlight Ready! 3 Mo. Free!!
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Add complete SSH and SFTP support to your .NET framework application
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Virtual File System SDK
Create your own file systems in Windows and .NET applications
Create your own file systems in Windows and .NET applications