C# 3.0 Automatic Properties

Defining properties can be very tedious. Suppose you have a class

1: public class UserProfile

2: {

3: private int userrId;

4: private string firstName;

5: private string lastName;

6:  

7: public int UserId

8: {

9: get { return this.userId; }

10: set { this.userId = value; }

11: }

12:  

13: public string FirstName

14: {

15: get { return this.firstName; }

16: set { this.firstName = value; }

17: }

18:

19: public string LastName

20: {

21: get { return this.lastName; }

22: set { this.lastName = value; }

23: }

24:  

25: public UserProfile() { }

26: }

It might not look like a lot of code, but when you think about the steps you need to go through to write that small amount of code, it can add up.

  1. Decide on the public property name.
  2. Declare the appropriately named and typed backing variable.
  3. Implement the corresponding public property.

Fortunately, C# 3.0 provides a way to simplify this. Now, we can write the class this way:

1: public class UserProfile

2: {

3: public int UserId { get; set; }

4: public string FirstName { get; set; }

5: public string LastName { get; set; }

6:  

7: public UserProfile() { }

8: }

This is certainly much more compact and requires fewer steps. When the compiler sees this class, (according to Reflector) it translates it to:

1: public class UserProfile

2: {

3: [CompilerGenerated]

4: private int k_BackingField;

5: [CompilerGenerated]

6: private string k_BackingField;

7: [CompilerGenerated]

8: private string k_BackingField;

9:

10: public int UserId

11: {

12: [CompilerGenerated]

13: get

14: {

15: return this.k_BackingField;

16: }

17: [CompilerGenerated]

18: set

19: {

20: this.k_BackingField = value;

21: }

22: }

23:  

24: public string FirstName

25: {

26: [CompilerGenerated]

27: get

28: {

29: return this.k_BackingField;

30: }

31: [CompilerGenerated]

32: set

33: {

34: this.k_BackingField = value;

35: }

36: }

37:

38: public int LastName

39: {

40: [CompilerGenerated]

41: get

42: {

43: return this.k_BackingField;

44: }

45: [CompilerGenerated]

46: set

47: {

48: this.k_BackingField = value;

49: }

50: }

51:  

52: public UserProfile() { }

53: }

There are a few things to look out for when using this syntax. The compiler forces you to declare properties with both a get and a set. You also don't get any kind of "safety" features, such as ensuring that you don't allow a null value to be assigned to a string property. If you want these more "advanced" features, you'll still need to define your property the "old school" way.

This article is part of the GWB Archives. Original Author: Scott Dorman

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.