Reputation: 3012
In C# I'm confused about properties. Specifically when you set get and set as just { get; set;}
. What does it mean when you do that? For example I have class property:
public Dictionary<string, string> clientDict { get; set; }
I've seen properties where they actually define get and set. I also understand that removing set makes it read only. But what happens in this case? Does it just use the default accessors of a normal dict?
Upvotes: 2
Views: 1027
Reputation: 19511
when you write a property like the previous one.
public Dictionary clientDict { get; set; }
the compiler translates it to the following
private Dictionary<string, string> _clientDic;
public Dictionary<string, string> clientDic
{
get { return _clientDic; }
set { _clientDic = value; }
}
and when you write a property like the following one
public int X {get;}
the compiler translates it to the following
private int _x;
public int X{
get { return _x; }
}
Upvotes: 3
Reputation: 15314
In C# I'm confused about properties. Specifically when you set get and set as just { get; set;}. What does it mean when you do that?
It means you're defining an Automatic Property
, and behind the scenes, the compilers creates a private, anonymous backing field
that can only be accessed through the property's get and set accessors.
So, your next question might be...
What's a
backing field
?
A backing field
is a field
which a property
references to store and retrieve values from.
You've seen what an automatic property
looks like, here's what a property
with a backing field
looks like...
private string name; // Backing field
public string Name // Property
{
get { return this.name; }
set { this.name = value }
}
I've seen properties where they actually define get and set. I also understand that removing set makes it read only.
Yes it makes the Property
read-only, but be careful, as C#
also contains a keyword
called readonly
which is set on Fields
. E.g. private readonly string name;
..
Just as properties can be read only
, they can also be write only
, if you remove the Get
accessor, and only leave the Set
accessor, clients will only be able to write to it.
But what happens in this case? Does it just use the default accessors of a normal dict?
The dictionary example you've given above will have a backing field... Clients will be able to write to it and read from it.
We could change things up if we wanted however...
Imagine that we're providing the scores of a live football game, giving write
access to clients would be a nightmare... so we could restrict that and only expose a get
accessor, whilst keeping the set
accessor private.
public int TeamFoo { get; private set; }
public int TeamBar { get; private set; }
Or, we could do it with an explicitly defined backing field...
private int teamFoo;
public int TeamFoo { get { return teamFoo; } }
Upvotes: 0
Reputation: 16718
You may be confusing the accessors to a class member with the accessors to a collection. As a commenter noted, the MSDN page on Auto-Implemented Properties explains that this:
public Dictionary<string, string> clientDict { get; set; }
Is equivalent to this:
private Dictionary<string, string> _clientDict;
public Dictionary<string, string> clientDict
{
get { return _clientDict; }
set(Dictionary<string, string> value)
{
_clientDict = value;
}
}
Those accessors just return a reference to the collection. They aren't passing anything through (as implied by your question "Does it just use the default accessors of a normal dict?"), and they are unrelated to the Dictionary<T>
class's []
and .Add()
methods.
When you access the Dictionary through the property:
var foo = clientDict["SomeKey"];
That will first return the result of the clientDict
* property's access, namely, a reference to _clientDict
, and will then index into that dictionary, returning the resulting value (assuming the key exists) and assigning it to foo
.
Please comment or edit the question if something further is confusing you about auto-properties.
* By the way, it's taking everything I've got not to write ClientDict
as the name of the property, since the C# convention is to capitalize property names just like method names :)
Upvotes: 3
Reputation: 4490
public Dictionary<string, string> clientDict { get; set; }
Is equivalent to defining get
and set
manually. The compiler handles all of it for you behind the scenes. So your above would become:
private Dictionary<string, string> _clientDict;
public Dictionary<string, string> clientDict;
{
get { return _clientDict; }
set { _clientDict = value; }
}
Upvotes: 2
Reputation: 161821
Simplify it. What would it mean if it were int
instead of Dictionary
?
public int ClientInt {get;set;}
There are no accessors for int
, so you wouldn't find yourself asking that question.
Upvotes: 1