Alex Rivera | Logout

Getters, setters, and properties best practices. Java vs. C#

Asked 2011-02-09T18:18:14.500
108

I'm taking a C# class right now and I'm trying to find out the best way of doing things. I come from a Java background and so I'm only familiar with Java best-practices; I'm a C# novice!

In Java if I have a private property, I do this;

private String name;

public void setName(String name) {
   this.name = name;
}

public String getName() {
   return this.name;
}

In C#, I see that there are many ways of doing this.

I can do it like Java:

private string name;

public void setName(string name) {
   this.name = name;
}

public string getName() {
   return this.name;
}

Or I can do it this way:

private string name;

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

Or:

public string Name { get; set; }

Which one should I use, and what are the caveats or subtleties involved with each approach? When creating classes, I am following general best-practices that I know from Java (especially reading Effective Java). So for example, I am favoring immutability (providing setters only when necessary). I'm just curious to see how these practices fit in with the various ways of providing setters and getters in C#; essentially, how would I translate best-practices from the Java world into C#?

EDIT

I was posting this as a comment to Jon Skeet's answer but then it got long:

What about a non-trivial property (i.e., with significant processing and validation perhaps)? Could I still expose it via a public property but with the logic encapsulated in get and set? Why would/should I do this over having dedicated setter and getter methods (with associated processing and validation logic).

Edit
Report

2 Answers

5

In C# favor properties for exposing private fields for get and/or set. The thie form you mention is an autoproperty where the get and set automatically generate a hidden pivot backing field for you.

I favor auto properties when possible but you should never do a set/get method pair in C#.

answered 2011-02-09T18:22:38.703
5
public string Name { get; set; }

This is simply a auto-implemented property, and is technically the same as a normal property. A backing field will be created when compiling.

All properties are eventually converted to functions, so the actual compiled implementation in the end is the same as you are used to in Java.

Use auto-implemented properties when you don't have to do specific operations on the backing field. Use a ordinary property otherwise. Use get and set functions when the operation has side effects or is computationally expensive, use properties otherwise.

answered 2011-02-09T18:22:50.887

Your Answer