C# 3.0 Auto-Properties - Useful or Not

C# 3.0 auto-properties — useful or not?

We use them all the time in Stack Overflow.

You may also be interested in a discussion of Properties vs. Public Variables. IMHO that's really what this is a reaction to, and for that purpose, it's great.

C# - auto-properties VS pre-3.0 properties

The syntax below is called auto properties, it doesn't matter in the terms of file size since in compilation time, a field is generated anyway (see, decompilation in the end of the answer) and there are get and set methods in the compilation results in both cases.

Auto properties allow you to keep your code more organized and short which is good for your code maintainability and readability, therefore you should prefer them when possible.

We will put aside the "In field without auto-property you can assign default value" topic for a second (also, it is possible now in auto-properties too in c# 6.0), sometimes, you want to run some more code inside the get or set methods of the property, like invoking event handles or validating the values, that's where standard property declaration comes into the picture, for example:

private int mNumber;

public int Number
{
get
{
return Number;
}
set
{
if (Number == 8)
{
throw new CannotReceive8Exception();
}
else
{
mNumber = value;
}
}
}

If you look at the decompiled code of this code:

public int Number { get; set; }

You will see that the compiler has added a background private field anyway:

Fields

What are Automatic Properties in C# and what is their purpose?

Automatic Properties are used when no additional logic is required in the property accessors.

The declaration would look something like this:

public int SomeProperty { get; set; }

They are just syntactic sugar so you won't need to write the following more lengthy code:

 private int _someField;
public int SomeProperty
{
get { return _someField;}
set { _someField = value;}
}

C# Automatic Properties

Properties can have code put into them without breaking contract, fields can't have code put into them without changing them to properties (and breaking the interface). Properties can be read only or write only, fields can't. Properties can be data bound, fields can't.

what is the exact use of auto implemented properties

Having a field inside a class is not a good idea. using properties allows you to encapsulate your data better. and when you just want to have a field accessible without any logic in you class then you can use automatic properties.

there are many scenarios that using a field inside your class makes things go wrong and harder as your software evolve.

for example: assume you have

public class C
{
public int Value;
}

in your code base.

then suddenly you realize that Value can not be set to zero. Then you have to make Value private and provide a SetValue() and GetValue() method. This is easy. but wait, how you gonna handle all other codes that relies on Value now?

but think about this one

public class C
{
public int Value { get; set; }
}

now it just needs a backing field like _value and implementing the setter and getter.

C#3.0 Automatic properties with extra logic

No, automatically implemented properties have no declared implementation. Any extended implementation that you wish to provide would have to use a regular property.

I am not sure what you are looking for in terms of an alternative - the syntax you have used in your question is the alternative.

Difference between Automatic Properties and public field in C# 3.0

Because they are implemented differently in the resulting IL code (and machine language). An Automatic property is still exposed as a public getter and setter, whereas a public field is just that - a single field..

Thus, implementing an auto property allows you at some later date to change the internal behavior of either the getter or setter (like adding a validator) without recompiling or re=coding any dependant classes that use it...

c# auto property VS 2005?

Automatically implemented properties and object initializers (the sample you showed) were both introduced in C# 3.0. Visual Studio 2005 only compiles C# 2.0.

For more on what came in which version (and which C# 3.0 features you can use when targeting .NET 2.0), see my article on the topic.

Problems with auto-properties

I don't think MS should specify a name - I think it's more reasonable to avoid binary serialization, which is always going to be somewhat brittle in my opinion. (Java serialization is similarly brittle.) If it relies on field names (and there may be a way of getting round that by attributing your properties - I'm not sufficiently experienced with binary serialization to know) then it's restricting implementation changes quite nastily already.

My biggest problem with automatic properties is that there's no way of creating genuinely readonly properties (with readonly backing fields). I've ranted about this before though...



Related Topics



Leave a reply



Submit