Home » c# » CSharp adds the var keyword!

CSharp adds the var keyword!

There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files.  It will not significantly reduce the amount of code that we have to write.

One of those language features is the ability to create properties, which we looked at last week.

Another of those features is the new var keyword.

So, instead of writing:

    MyClass c = new MyClass();

 

you can now write:

    var c = new MyClass();

 

Which isn’t a lot of code until you start qualifying the Class name with namespaces.

During the beta cycle, I saw a demo that let you declare a variable, var c, and then several lines later initialize it with, new MyClass(), which gave the appearance that var was more like the var keyword in javascript, and therefore a variant than what it really is.

In the release version of CSharp 3.0, if you use the var keyword to declare a variable, you MUST initialize it on the same line, or you will get a compiler error.  I suppose it makes writing the compiler a whole lot easier this way too.

One other small thing to note, which should be obvious by now.  Since we have to initialize the variable to some object or value, and since we can’t initialize it anywhere other than on the line it is declared on, you can’t treat the variable as a variant.  A variable declared as var is as strongly typed as any other variable you would create.  So, if I did something like this:

    var c = new MyClass(); 
    c = "Some string here";

I would get a compiler error because I’m trying to assign a string type to a MyClass variable.  var does not stand for “variant,” it stands for “variable.”  All the compiler does when it sees this is look at the type being assigned to the variable and replaces the var keyword with that type.

So, when the compiler processes the code, it takes this:

        var c = new MyClass();

and turns it into this:

MyClass c = new MyClass();

Finally, it may be helpful to point out here that this whole process happens at compile time.  This should be obvious by the fact that this works in both .NET 2.0 code compiled with the CSharp 3.0 compiler as well as .NET 3.x code.  But, sometimes the obvious isn’t obvious until someone states it explicitly.

 

Other post in c#

Related Post

  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]
  • Simple Properties in C# 3.5Simple Properties in C# 3.5 It's such a little thing.  But, how much of our CSharp code looks something like this: private string _propertyName; public string PropertyName { get { […]
  • The VB.NET Ternary OperatorThe VB.NET Ternary Operator I think the VB.NET Ternary Operator may be the last operator that I really miss in VB.NET from my curly brace language experience.  Although, I have to admit, I wouldn't have missed it all […]
  • Computed Columns Using LINQ to SQL ClassesComputed Columns Using LINQ to SQL Classes Last week we looked at the extension points Microsoft has wired into the LINQ to SQL classes and how they can be used to achieve some of the capabilities of the Business Logic Layer (BLL) […]
  • Assign Multiple enum Values To One VariableAssign Multiple enum Values To One Variable I saw this question and immediately thought, "You can't!  An Enum is an Integer that has been restricted to the values it can accept." And I was basically right.  But, […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.