简体   繁体   中英

C# Auto-Implemented Properties Assistance

In C# I'm confused about properties. Specifically when you set get and set as just { get; set;} { 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?

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 :)

  1. this is called "Automatic Properties". (when you just write the set and the get method without any code inside them)
  2. the target of the Automatic Properties is the simplifying the coding process, and make it faster.
  3. 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; }
 }
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; } 
}

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.

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 . Eg 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; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM