Expert Texture Home Contact me About Subscribe Digipede Connect on LinkedIn rwandering on Twitter rwandering on FriendFeed

The blogged wandering of Robert W. Anderson

Automatic Properties in C# 3.0

While I haven’t had a chance to play with Orcas, I have been following the new language features.  Scott Guthrie and Bart De Smet talk about the new automatic property feature in recent blog entries.

Basically, automatic properties allow you to replace this common pattern:

private string name;

public string Name {
    get {
        return name;
    set {
        name = value;


public string Name { get; set; }

Very cool.  I’m all for writing less code (not because I’m lazy but because it means fewer lines of code to maintain).  Even better, it allows you to change to the old property pattern when necessary without breaking your public contract.

But, I do have two questions:

Binary serialization

I was a little surprised to find the way the underlying automatic field is named.  In Bart’s example the public property is Name.  The underlying field generated by C# is <>k__AutomaticallyGeneratedPropertyField0.  This most likely won’t conflict with any other fields you might have in your class. 

So far so good.

But, what about binary serialization?  If you attribute your class [Serializable] and want to use the default serialization features of .NET without implementing ISerializable, then it becomes hard to get the full benefit out of the automatic property.

Why?  Because if you need to convert from automatic properties to the old pattern, your class will no longer be able to deserialize previously persisted objects (without writing a bunch more heinous code). 

Question 1: Is there an attribute to set the automatic field name?


To make it easier for our customers to debug their code with Digipede’s public APIs, we use the following attribute on all of our private fields:


This makes it so the debugger will not display both the duplicate values of the private field and public properties.

Question 2: Is there an attribute you can put on the automatic property to hide the underlying field or is it automatic?

I suppose I should install Orcas and answer these questions myself , but in the interim, do any readers know the answers?



    Mohamed Saleh wrote @ August 27th, 2007 at 5:34 am

u mention to a very good point, can u explain more the second question! i dont get it, what u mean exactly?

    Expert Texture » Automatic Properties Follow-up wrote @ August 27th, 2007 at 10:48 am

[…] in March, I posted Automatic Properties in C# 3.0 which included a couple of questions.  At the time I hadn’t installed any Orcas […]

    Robert W. Anderson wrote @ August 27th, 2007 at 10:50 am

Mohamed, thanks for the comment. I’ve gone further with a follow-up post. Your question is answered there.


Your comment

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>