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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Carl TawnSystems 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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anarki_jimbelSenior DeveloperCommented:
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 :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.