简体   繁体   English

您经常看到滥用C#速记吸气剂/孵化器?

[英]How often do you see abuse of C# shorthand getters/setters?

In C# you can create getter/setters in a simpler way than other languages: 在C#中,您可以以比其他语言更简单的方式创建getter / setter:

public int FooBar { get; set; }

This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. 这将创建一个内部私有变量,您无法直接使用外部属性“FooBar”直接访问它。

My question is - how often do you see this abused? 我的问题是 - 你多久经常看到这种滥用? It seems like it has a high potential to violate encapsulation best-practices often. 它似乎很有可能经常违反封装最佳实践。 Don't get me wrong, I use it as appropriate, and partial variations of it for read-only write-only types of properties, but what are your unpleasant experiences with it from other authors in your code base? 不要误解我的意思,我会酌情使用它,并为只读的只写类型的属性使用它的部分变体,但是你的代码库中的其他作者对它的不愉快经历是什么?

Clarification: the intended definition of abuse would indeed be creating such a property when private variables are appropriate. 澄清:当私人变量适当时,滥用的预期定义确实会产生这样的属性。

I've seen it abused (in my opinion). 我看到它被滥用(在我看来)。 In particular, when the developer would normally write: 特别是,当开发人员通常会写:

private readonly int foo;
public int Foo
{ 
    get 
    { 
        return foo;
    }
}

they'll sometimes write: 他们有时会写:

public int Foo { get; private set; }

Yes, it's shorter. 是的,它更短。 Yes, from outside the class it has the same appearance - but I don't view these as the same thing, as the latter form allows the property to be set elsewhere in the same class. 是的,从类外面它具有相同的外观 - 但我不认为这些是相同的事情,因为后一种形式允许在同一个类的其他地方设置属性。 It also means that there's no warning if the property isn't set in the constructor, and the field isn't readonly for the CLR. 它还意味着如果未在构造函数中设置该属性,则不会发出警告,并且该字段对于CLR而言不是只读的。 These are subtle differences, but just going for the second form because it's simpler and ignoring the differences feels like abuse to me, even if it's minor. 这些是微妙的差异,但只是为了第二种形式,因为它更简单,忽略差异感觉就像虐待我,即使它是次要的。

Fortunately, this is now available as of C# 6: 幸运的是,现在可以从C#6开始使用它:

// Foo can only be set in the constructor, which corresponds to a direct field set
public int Foo { get; }

There is no "abuse" in simply not writing the field manually; 只是不手动编写字段就没有“滥用”; and it is good to encourage all access via the property (not directly to the field) anyway! 无论如何,鼓励所有通过财产(不是直接到现场)的访问是好的!

The biggest problem I know of is with binary serialization , where it gets a bit tricky to change back to a regular field without making it version-incompatible - but then... use a different serializer ;-p 我所知道的最大的问题是二进制序列化 ,在不使其版本不兼容的情况下更改回常规字段会有点棘手 - 但随后......使用不同的序列化程序;-p

It would be nice if there was a "proper" readonly variant, and if you didn't need to use :this() ctor-chaining on structs, but.... meh! 如果有一个“正确的”只读变体,并且如果你不需要使用:this() ctor-chaining on structs,那将是很好的,但是...... meh!

I haven't seen any abuse of it. 我没有看到任何滥用它。 And to be honest, I don't really see what you mean, as I can't see how this syntax could be abused. 说实话,我真的不明白你的意思,因为我看不出这种语法是如何被滥用的。

I don't think automatic properties are any worse than regular properties in regards to encapsulation. 我不认为自动属性在封装方面比常规属性更差。

If you mean that some developers use public automatic properties instead private fields then this is of course wrong and breaks encapsulation.. 如果你的意思是一些开发人员使用公共自动属性而不是私有字段,那么这当然是错误的并打破了封装。

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

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