0

Say you have a class test:

public class test
{
    public test()
    {
        isTesting = false;
    }

    private bool isTesting;

    public bool getTesting() { return isTesting; }
    private void setTesting(bool testing) { isTesting = testing; }
}

and another one:

public class test
{
    public test()
    {
        isTesting = false;
    }

    public bool isTesting { private set; get; }
}

Is there any difference between these two?

Should one be used over the other or is this a matter of preference?

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • the only difference is your first code is java-like. You don't need to declare getter-setter methods yourself, let the compiler worry about that. – Selman Genç Aug 29 '14 at 11:31
  • @Marcus that question doesn't include the option whether to use getters/setters or properties – Hatted Rooster Aug 29 '14 at 11:33
  • No, but it describes how properties are used on the .NET platform and what the difference is. Assuming your viewpoint in this question I propose that you can find your answer in the supplied thread. – Marcus Aug 29 '14 at 11:38

3 Answers3

0

With .NET you should go with properties. Usage of GetX/SetX methods as used in Java is discouraged. When exposing data of a type, properties are generally recommended (they define a public interface) – fields should only ever be private.

knittl
  • 246,190
  • 53
  • 318
  • 364
0

Private property have an advantage in .Net as they allows you to abstract your internal data so that when there are changes in the internal representation then they dont affect other parts of the implementation. This is not provided in Private Fields.

On a side note private property are very advantageous in case of lazy load.

You may also check Are there any reasons to use private properties in C#?

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The two code samples you gave are functionally equivalent right now, but the main reason for getter / setter methods (whether they're in the form of "properties" or explicit methods) is that their implementation can be changed without modifying the code that calls them. If you did that, these would not be equivalent, because the first one doesn't actually call the setter and the second one does. To match the second, the first constructor should be written as follows:

public test()
{
    setTesting(false);
}

Or the second should be written to not call the setter, i.e. set the field instead of the property:

public test()
{
    _isTesting = false;
}

private bool _isTesting;

public bool isTesting {
    private set { _isTesting = value; }
    get { return _isTesting; }
}

Other than that, there is no practical difference -- in most cases: Even though properties are essentially compiled into getter and setter methods anyway, they still retain metadata that they are in fact "properties" as opposed to "methods", and some APIs rely on this information.

The other difference is semantics: Properties are meant to be treated as if they are fields, implying that they do some kind of basic storage / retrieval from memory. If I see a method named GetX as opposed to property X in C# code, I'll probably assume that the method is doing something more advanced and treat it differently.

nmclean
  • 7,564
  • 2
  • 28
  • 37