Ehsan Tavakoli

Writing About .NET Programming and other stuff

Archive for the ‘Implicitly Typed Local Variable’ Category

How to use Implicitly Typed Local Variables and Auto-Implemented Properties

leave a comment »

Today I want to write a very simple sample about how we can use implicitly typed local variable and also Auto-Implemented properties. As you may know, these two new features (and few others) have been introduced by Microsoft in C# 3.0. And to be honest they are quite handy in many solutions. First see what they mean. With Implicitly Typed Local Variables, you can define a local variable without specified what its type is. You should use var keyword to instruct the completer to get the type from expression on the right side. These types can be built-in types or anonymous type or even user-defined type.
Speaking of Anonymous type is not bad to glance over what it means. Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler. Here is an example:

var v = new { Amount = 108, Message = "Hello" };

Here we defined a variable name v with our anonymous type which has two properties (Amount and Hello). As you noticed, we don’t need to specified the types of properties and we used var keyword to instruct the compiler to grasp our new anonymous type and assign it to our variable.

Back to another new feature “Auto-Implemented Properties”, In C# 3.0 auto-implemented properties make property declaration more simple when no additional logic is required in the property accessors. for example:

class ProcessData
{
    public Int32 Id { get; set; }
    public Int64 Memory { get; set; }
    public String Name { get; set; }
}

as you can see here we just implemented our class ProcessData with three properties and their setters and getters.

I found a very good example in book “Linq in Action”. here is the example with a little modification:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ConsoleApplication1
{
    class SampleClass
    {
        class ProcessData
        {
            public Int32 Id { get; set; }
            public Int64 Memory { get; set; }
            public String Name { get; set; }
        }
        static void DisplayProcesses()
        {
            var processes = new List<ProcessData>();
            foreach (var process in Process.GetProcesses())
                processes.Add(new ProcessData{Id = process.Id, Name = process.ProcessName, Memory = process.WorkingSet64});
            ObjectDumper.Write(processes);
        }
        static void Main()
        {
            DisplayProcesses();
            Console.ReadKey();
        }
    }

}

As you can see here in this code, I used Implicitly Typed Local Variables (processes) and also Auto Implemented Properties (for class ProcessData) and also initialized ProcessData in new format (object initialiser) that doesn’t need to call its constructor (which it doesn’t have). And also please take note that I used ObjectDumper class which it’s provided by Microsoft as part of the LINQ code samples.

ObjectDumper can be used to dump an object graph in memory to the console. It’s particularly useful for debugging purposes. You can find its source code here: http://code.msdn.microsoft.com/wpfobjectdumper

Written by Ehsan Tavakoli

September 7, 2010 at 10:50 pm