C# getter and setters


Could you advise the difference and whether one method is preferred for the following gett and setter methods...

public String myString { get; set; }


// Getter
public setMyString (String astring) {
 myString = aString

// Setter
public getString() {
   return myString;

Am I correct in thinking that public methods would be delcared if myString has private accessibility?
Who is Participating?
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Getters and Setters are harking back to C++. Properties are a shorthand way of doing the same thing.

There are two versions in C# you can either create a private member variable and expose it as a property, or you can use the new Automatic property feature. Using properties rather than explicit getter and setter methods is more convenient and will make your code more readable.

Automatic properties are quick if you don't need to perform any custom validation as part of the get/set routines.
// standard property
private string myString; // member variable

public string MyString
    get { return myString; }
    set { myString = value; }

// automatic property
public string MyString { get; set; }

These two samples do exactly the same thing, the automatic property version is simply shorthand.

Open in new window

rg20Connect With a Mentor Commented:
The first example is a c# feature, they figured that if you are declaring a property with getters and setters, then why should you have to declare a private variable that will never be accessed outside of the class.  If you are doing manipulations on the value before you set it, then you need the private variable.

For example

if you need to determine if a value is null before you set the class member then you need the private variable to do that against.  but if you need to do that, the class is not the place for it anyway
TribmosConnect With a Mentor Commented:
I used to use set/get public methods for things, but have found a get/set routine to be much cleaner
You can do additional checks on the get/set routine (or even other method calls) its basically a single property that can do both.
private (or protected) String _myString;
public String myString
  get { return this._myString; }
  set { this._myString = value;

Open in new window

anarki_jimbelConnect With a Mentor Commented:
When someone comes from a different background he takes his customs and preferences with him. After Java experience for me these get-set properties were a bit unusual, and I preferred to use methods. Later I changed to properties, like Tribmos, I also found a get/set routine to be much cleaner.

Also, see the MS guidelines to choose between properties and methods:

The main point: if it relates to data (Name, Price) - use properties, if it relates to actions  - use methods.

Eventually, both ways give the same result :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.