0

I am learning about properties and I have a rather simple question:

Are properties just variables with "build-in" getter and setter?

What I mean can be described with this example.

int variable;

public void SetVariable(int _value)
{
    variable = _value;
}

public int GetVariable()
{
    return variable;
}

int variable { get; set; }

Are those two exactly the same or there is some slight difference that I don't see?

FICHEKK
  • 527
  • 1
  • 4
  • 17
  • 2
    Aside from naming, they're the same. Properties are actually implemented with getter and setter methods with special names that are hidden for your convenience. Note that you should never use the `Get/Set` approach in .NET *instead* of a property, because that "property" will not be discoverable to clients and tools that know about capital-P properties. – Jeroen Mostert Aug 15 '18 at 14:01
  • 1
    The different 'flavors' of C# properties are explained in detail [in the documentation](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) – jeroenh Aug 15 '18 at 14:02
  • @JeroenMostert Thank you, that answered my question. – FICHEKK Aug 15 '18 at 14:04

2 Answers2

3

They are represented differently in the class. This is of practical importance if you use reflection, or if you use tools that use reflection. (You second example will show up in PropertyInfo, while your first would have to be found through FieldInfo plus MethodInfo plus application of some convention, a la bean conventions in Java.)

Because most developers most of the time don't deal directly with reflection, and because at best they don't think much about the indirect uses, it's easy to think of properties as just syntactic sugar around "field + getter + setter", but it can make a difference.


Edit: weirdly, when I initially answered I missed the (arguably) more important difference, which is how these things are used once declared. Yes you get (mostly) the same moving parts, but

In your first example,

variable = 37;

is a direct assignment that bypasses the setter logic. For this reason, it's likely you would declare variable as private and make the getter/setter public; so calling code would typically have to say

SetVariable(37);

instead.

In your second example, saying

variable = 37;

would call the variable's set method with the value 37.

Now again, this may seem meaningless since you're using the default setter in your example, but that needn't always be the case. It could be as simple as thread safety, or as complex as the value being transformed in some way rather than stored directly in an internal field.

And in the end you could still say it's syntactic sugar, but now it affects every bit of code that touches the variable, rather than just declaration of the variable itself.

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41
0

In general, methods represent actions.

Properties represent data, Properties are meant to be used like fields, that meaning properties should not be computationally complex or produce side effects.

When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.

Muhammad Hassan
  • 447
  • 2
  • 12