简体   繁体   English

C#自动实现的属性帮助

[英]C# Auto-Implemented Properties Assistance

In C# I'm confused about properties. 在C#中,我对属性感到困惑。 Specifically when you set get and set as just { get; set;} 特别是当你设置get并设置为just { get; set;} { get; set;} . { get; set;} What does it mean when you do that? 这样做意味着什么? For example I have class property: 例如,我有class属性:

public Dictionary<string, string> clientDict { get; set; }

I've seen properties where they actually define get and set. 我已经看到了他们实际定义get和set的属性。 I also understand that removing set makes it read only. 我也明白删除set会使它只读。 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: 正如评论者所指出的, Auto-Implemented Properties上的MSDN页面解释了这一点:

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. 它们没有传递任何东西(正如你的问题所暗示的那样“它只是使用普通字典的默认访问器吗?”),它们与Dictionary<T>类的[].Add()方法无关。

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 . 这将首先返回clientDict *属性访问的结果,即对_clientDict的引用,然后将索引到该字典中,返回结果值(假设密钥存在)并将其分配给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 :) *顺便说一下,它正在把我所有的东西都没有写入ClientDict作为属性的名称,因为C#约定是像方法名一样大写属性名称:)

  1. this is called "Automatic Properties". 这称为“自动属性”。 (when you just write the set and the get method without any code inside them) (当你只是编写set和get方法时,里面没有任何代码)
  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; 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. 相当于手动定义getset 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 ? 如果它是int而不是Dictionary这意味着什么?

public int ClientInt {get;set;}

There are no accessors for int , so you wouldn't find yourself asking that question. int没有访问器,所以你不会发现自己在问这个问题。

In C# I'm confused about properties. 在C#中,我对属性感到困惑。 Specifically when you set get and set as just { get; 特别是当你设置get并设置为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. 这意味着您正在定义Automatic Property ,并且在幕后,编译器会创建一个私有的匿名backing field ,只能通过属性的get和set访问器进行访问。


So, your next question might be... 所以,你的下一个问题可能是......

What's a backing field ? 什么是backing field

A backing field is a field which a property references to store and retrieve values from. backing fieldproperty引用以存储和检索值的field

You've seen what an automatic property looks like, here's what a property with a backing field looks like... 你已经看到了automatic property样子,这里有一个带有backing fieldproperty看起来像......

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. 我已经看到了他们实际定义get和set的属性。 I also understand that removing set makes it read only. 我也明白删除set会使它只读。

Yes it makes the Property read-only, but be careful, as C# also contains a keyword called readonly which is set on Fields . 是的,它使该Property只读,但要小心,因为C#还包含一个名为readonlykeyword ,它在Fields上设置。 Eg private readonly string name; 例如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. 就像属性可以read only ,它们也可以write only ,如果你删除了Get访问器,只留下了Set访问器,客户端只能写入它。

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. 想象一下,我们提供了一个现场足球比赛的分数,给客户write访问权限将是一场噩梦...所以我们可以限制它,只暴露一个get访问器,同时保持set accessor私有。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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