Reputation: 142
By what I've read I'm sure I'm going through the same process that most went through when jumping into WPF. I am coming from embedded im struggling.
take these examples
int foo; //
int foo { get; set; } //c#
public int foo{ get => foo; set => foo= value; }
private int _foo;
public int foo{ get => _foo; set => _foo= value; }
Could someone, please give me a definitive answer as to why all the getters and setters.
- is it just a visual studio, and its autocomplete settings?
- is it because of threading and foo is getting marked as volatile behinds the scenes?
- or is something else going on.
Follow up
for anyone learning and struggling with data binding, the links in "580's" reply under "binding" are by far the best examples i have come across, to get your head around the syntax. Also i came across "ReSharper" for visual studio which lets you know about errors and the correct way to fix, using the correct methods. using both the examples and resharper i have learnt more in 3 hrs than i had in the last week.
Upvotes: 1
Views: 4007
Reputation: 480
The get and set syntax in this case indicates what in C# is known as properties. Properties are not specific to WPF, Visual Studio, threading or the volatile keyword. Properties are simply a construct of the C# language. MSDN - Properties
I'm assuming a C/C++ background here if you're coming from the embedded world. Plain old standard member variables in C# are known as fields. Properties represent member variables with a bit of a twist.
Definitive answer on why the get and set keywords are found everywhere is due to their use in properties. Properties are widely used because they are typically considered better practice then using fields in most scenarios. They allow for more fine-grained access control. I would suggest reading through this article by Jon Skeet C# In Depth - Why Properties Matter
Property vs Field
Now for some detail, properties differ from fields in that they may define additional functionality to perform every time a read (get) or modification (set) occur. This functionality happens implicitly such that the consumer of the property may not even be aware of it. This layer of abstraction is one reason why properties have been deemed more valuable then fields.
While not necessarily the case all the time a property typically encapsulates a private field, traditionally of the same type. When the standard syntax { get; set; }
is used the compiler will automatically generate a private field of the same type as the property. This is known as an auto property. Example:
public string Title { get; set;}
would be converted into the following by the C# compiler.
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
If you were to manually/explicitly define the private field yourself then the field becomes known as a backing field also sometimes referred to as a backing store. Two of your examples #3 and #4 represent this, where #4 does so more evidently.
As stated earlier not all properties require encapsulating a field. Here is an example of a property that just returns a string literal.
public string RandomText { get { return "Foo"; } }
Now onto your examples:
Binding
Helpful reading on data binding. MSDN - Data Binding
Upvotes: 7