您多久会看到滥用 C# 速记 getter/setter 的情况?

发布于 2024-07-25 19:20:24 字数 342 浏览 7 评论 0原文

在 C# 中,您可以用比其他语言更简单的方式创建 getter/setter:

public int FooBar { get; set; }

这将创建一个您无法直接寻址的内部私有变量,可以使用外部属性“FooBar”直接访问它。

我的问题是 - 您多久会看到这种行为被滥用? 看起来它很有可能经常违反封装最佳实践。 不要误会我的意思,我根据需要使用它,并将其部分变体用于只读只写类型的属性,但是您的代码库中的其他作者对它有哪些不愉快的经历?

澄清:滥用的预期定义确实是在私有变量合适时创建这样的属性。

In C# you can create getter/setters in a simpler way than other languages:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愿与i 2024-08-01 19:20:24

我见过它被滥用(在我看来)。 特别是,当开发人员通常会写:

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

他们有时会写:

public int Foo { get; private set; }

是的,它更短。 是的,从类的外部来看,它具有相同的外观 - 但我不认为它们是同一件事,因为后一种形式允许在同一类的其他地方设置该属性。 它还意味着,如果未在构造函数中设置该属性,并且该字段对于 CLR 来说不是只读的,则不会发出警告。 这些都是微妙的差异,但只是选择第二种形式,因为它更简单,忽略这些差异对我来说就像是虐待,即使它很小。

幸运的是,从 C# 6 开始可以使用此功能:

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

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

// Foo can only be set in the constructor, which corresponds to a direct field set
public int Foo { get; }
我是男神闪亮亮 2024-08-01 19:20:24

简单地不手动编写该字段并不构成“滥用”; 无论如何,鼓励所有人员通过酒店进入(而不是直接进入现场)是件好事!

我知道的最大问题是 二进制序列化,其中在不使其版本不兼容的情况下改回常规字段会有点棘手 - 但是然后...使用不同的序列化器;-p

如果有一个“适当的”只读变体,并且如果你没有'不需要在结构上使用 :this() 构造函数链接,但是.... 嗯!

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

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!

方圜几里 2024-08-01 19:20:24

我没有看到任何滥用它的情况。 老实说,我真的不明白你的意思,因为我不明白这种语法是如何被滥用的。

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.

蒲公英的约定 2024-08-01 19:20:24

我认为在封装方面自动属性并不比常规属性差。

如果您的意思是某些开发人员使用公共自动属性而不是私有字段,那么这当然是错误的并且破坏了封装。

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文