C# 3.0: Auto-Implemented Property

Published 17 July 07 09:22 PM | adrian

Tired of writing this?

private int id;
public int Id
{
 
get
 
{
   
return this.id;
  }
  set
  {
   
this.id = value;
 
}
}

Fear not! C# 3.0 will give way to a new structure for simple getter setter. The above code will become...

public string Test { get; set; }

Amazing!

Behind the scene, the compiler will generate an anonymous type as the backing field, and create getter/setter code as usual. Now what's anonymous type? Well, that's another question for another day!

Share this post: | | | |
Filed under:

Comments

# Maximilian Haru Raditya said on July 19, 2007 04:08 PM:

How if I wanted the auto-generated code (private int id;) to be (protected int id;)?

# adrian said on July 19, 2007 04:16 PM:

Well, you can't! (at least right now before Beta2)

This is for rapid development, when more complex scenario happens, you would just extend the definitions.

# NgeBlogDong!@INDC said on July 21, 2007 09:45 AM:

So earlier this week I've blogged about the auto-implemented property . A thing it uses is anonymous

# SyncMaster 550s said on July 24, 2007 08:09 PM:

Udah ada ebooknya mas? Bisa kasi tau URL nya utk yg beta 2 ini? Trims :)