Getter and Setter declaration in .NET
Properties are used to encapsulate some data. You could use a plain field:
public string MyField
But this field can be accessed by all outside users of your class. People can insert illegal values or change the value in ways you didn't expect.
By using a property, you can encapsulate the way your data is accessed. C# has a nice syntax for turning a field into a property:
string MyProperty { get; set; }
This is called an auto-implemented property. When the need arises you can expand your property to:
string _myProperty;
public string MyProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
Now you can add code that validates the value in your setter
:
set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException();
_myProperty = value;
}
Properties can also have different accessors for the getter and the setter:
public string MyProperty { get; private set; }
This way you create a property that can be read by everyone but can only be modified by the class itself.
You can also add a completely custom implementation for your getter
:
public string MyProperty
{
get
{
return DateTime.Now.Second.ToString();
}
}
When C# compiles your auto-implemented property, it generates Intermediate Language (IL). In your IL you will see a get_MyProperty
and set_MyProperty
method. It also creates a backing field called <MyProperty>k_BackingField
(normally this would be an illegal name in C# but in IL it's valid. This way you won't get any conflicts between generated types and your own code). However, you should use the official property syntax in C#. This creates a nicer experience in C# (for example with IntelliSense).
By convention, you shouldn't use properties for operations that take a long time.
With this, you can perform some code in the get
or set
scope.
private string _myProperty;
public string myProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
You also can use automatic properties:
public string myProperty
{
get;
set;
}
And .Net Framework will manage for you. It was create because it is a good pratice and make it easy to do.
You also can control the visibility of these scopes, for sample:
public string myProperty
{
get;
private set;
}
public string myProperty2
{
get;
protected set;
}
public string myProperty3
{
get;
}
Update
Now in C# you can initialize the value of a property. For sample:
public int Property { get; set; } = 1;
If also can define it and make it readonly, without a set.
public int Property { get; } = 1;
And finally, you can define an arrow function.
public int Property => GetValue();
1st
string _myProperty { get; set; }
This is called an Auto Property in the .NET world. It's just syntactic sugar for #2.
2nd
string _myProperty;
public string myProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
This is the usual way to do it, which is required if you need to do any validation or extra code in your property. For example, in WPF if you need to fire a Property Changed Event. If you don't, just use the auto property, it's pretty much standard.
3
string _myProperty;
public string getMyProperty()
{
return this._myProperty;
}
public string setMyProperty(string value)
{
this._myProperty = value;
}
The this
keyword here is redundant. Not needed at all. These are just Methods that get and set as opposed to properties, like the Java way of doing things.
Well, the first and second both generate something like the third in the end. However, don't use the third when you have a syntax for properties.
Finally, if you have no work to do in the get
or set
, then use the first.
In the end, the first and second are just some form of syntactic sugar, but why code more than what's necessary.
// more code == more bugs
And just to have a little fun, consider this:
public string A { get; private set; }
Now that's a lot more straight forward isn't it? The public
modifier is implied on both the get
and the set
, but it can be overriden. This would of course be the same rule for any modifier used when defining the property itself.