Automatic properties in C# 2008 (Language Feature)

The C# 2008 supports number of new constructs such as lambda expression, automatic properties, implicitly typed local variables, partial methods etc. Let’s examine automatic properties.

Up till now we have been encapsulating our private fields as

class TestClass
private string name;
public string Name
get { return name; }
set { name = value; }


This approach is good enough but let say if we require 20 fields as public interface then it requires a little effort of creating private field first and then encapsulating it as a public property.

As the word “automatic” refers, you can now create just properties and C# will automatically create a backing private field for you at compile time though it’s not visible to you.

class TestClass
public string Name { get; set; }

The automatic properties are initialized by default values i.e. integers are initialized with zero, strings with empty, reference type with null etc.

You can apply restricting access similarly as one can apply on normal properties such as

public string Address { get; private set; }

having public scope of get and private scope of set. However you cannot write read only fields as you previously like

public string Address { get; } //error

read only should be done by restricting access through access modifiers. We will access the automatic properties in the similar way as we did in case of normal properties.

TestClass tc = new TestClass();
tc.Name = “CD”;


The automatic properties are one of the good extensions C# 2008 provides, if you are just need a simple public property to access private fields outside class then automatic properties are the good alternative.

String vs. StringBuilder

Well some juniors told me that they have started C# so they want me to start some beginner level discussion on C# .NET

The first thing that came in my mind for novice was comparison between string and string builder class.

I don’t know whether you have noticed it or not.

In C# .NET strings are immutable and hence it also do not allow us to change it.

By immutable i mean, whenever we change the string it will create a new instance of it in memory rather than changing value on same memory location.

Let me clear this with the help of code.

Consider following code snippet :

string str= “Immutable”; //line 1
str += ” Test”; //line 2

when i appended ” Test” in str (in line 2), rather than appending it in the same instance, it will create another instance (somewhere else in memory) with value “Immutable Test” and str will now refer to “Immutable Test” in memory leaving the old one in memory unreferred for garbage collector.

It means that whenever we will change string, the .net framework will create a new one at any other place in the memory.

Plus because of this you are not allowed to change any string like
str[1]= ‘c’; because of immutability…

For the required behavior StringBuilder class is used.

String is faster than StringBuilder

thats all i want to say right now… Please correct me if i m wrong somewhere…

bye for now and keep saying

.NET Rocks :p