任何写“私人”的理由都可以。 C# 中的关键字?

发布于 2024-12-20 14:57:45 字数 506 浏览 1 评论 0原文

据我所知,private 是 C# 中默认的无处不在(这意味着如果我不写 publicprotected< /code>、internal 等。默认情况下它将是 private)。 (如果我错了,请纠正我。)

那么,编写该关键字的原因是什么,或者为什么它甚至为会员而存在?

例如,当自动生成事件处理程序时,它看起来像这样:

private void RatTrap_MouseEnter(object sender, CheeseEventArgs e)
{

}

但是如果这是隐含的和默认的,为什么它甚至会写为 private 呢?只是为了让新手开发人员(他们不知道这是 C# 默认值)知道它是私有的吗?或者编译器有什么区别?

此外,是否存在写入“private”(单独)改变成员的可访问性的情况?

As far as I know, private is the default everywhere in C# (meaning that if I don't write public, protected, internal, etc. it will be private by default). (Please correct me if I am wrong.)

So, what's the reason to write that keyword, or why does it even exist for members?

For example, when an event handler is auto-generated it looks like this:

private void RatTrap_MouseEnter(object sender, CheeseEventArgs e)
{

}

But why does it even write private if that's implied and default? Just so that novice developers (who don't know it's the C# default) know that it's private? Or is there a difference for the compiler?

Moreover, is there a case where writing "private" (alone) will change the accessibility of the member?

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

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

发布评论

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

评论(10

傲鸠 2024-12-27 14:57:45

AFAIK,私有是 C# 中所有地方的默认值(这意味着如果我不写 public、protected、internal 等,默认情况下它将是私有的)。 (如有错误请指正)

这不是真的。默认情况下,命名空间内定义的类型(类、结构、接口等)将是内部。此外,不同类型中的成员具有不同的默认可访问性(例如接口成员的公共)。有关详细信息,请参阅 MSDN 上的辅助功能级别

还,

那么,编写该关键字的原因是什么,或者为什么它存在?

明确指定这一点有助于非常明确地表明您将该类型设为私有的意图。随着时间的推移,这有助于提高代码的可维护性。这可以帮助其他开发人员(或您自己)了解成员是默认私有的还是故意的等。

AFAIK, private is the default everywhere in C# (meaning that if I don't write public, protected, internal, etc. it will be private by default). (please correct me if wrong).

This is not true. Types defined within a namespace (classes, structs, interfaces, etc) will be internal by default. Also, members within different types have different default accessibilities (such as public for interface members). For details, see Accessibility Levels on MSDN.

Also,

So, what's the reason to write that keyword, or why does it even exist?

Specifying this explicitly helps denote your intention to make the type private, very explicitly. This helps with maintainability of your code over time. This can help with other developers (or yourself) knowing whether a member is private by default or on purpose, etc.

说好的呢 2024-12-27 14:57:45

据我所知,私有是 C# 中所有地方的默认设置

不完全是 - 默认值是“此声明可用的最受限制的访问”。例如,对于顶级类型,默认值为 internal;对于嵌套类型,默认值为 private

那么,编写该关键字的原因是什么,或者为什么它存在?

它使其明确,这有两个原因:

  • 它使那些不知道默认值的人更清楚,根据你的问题(我个人从来不喜欢这个论点,但我认为值得一提)
  • 它给出给人的印象是您有意决定将其设为私有,而不是仅仅采用默认值。

至于你的最后一部分:

此外,是否存在写入“private”(单独)会改变成员的可访问性的情况?

是的,为了让一半的财产比另一半更受限制:

// Public getter, public setter
public int Foo { get; set; }

// Public getter, private setter
public int Bar { get; private set; }

曾经尽可能地采用默认值,但我已经确信(部分是由埃里克·利珀特(Eric Lippert)),明确表示你'我考虑过并决定将某些内容设为私有是个好主意。

就我个人而言,我希望有一种方法可以为密封/未密封的类型声明做到这一点 - 甚至可能没有默认值。我怀疑许多开发人员(包括我自己,如果我不小心的话)将类未密封只是因为这比密封它们更省力。

AFAIK, private is the default everywhere in C#

Not quite - the default is "the most restricted access available for this declaration". So for example, with a top-level type the default is internal; for a nested type the default is private.

So, what's the reason to write that keyword, or why does it even exist?

It makes it explicit, which is good for two reasons:

  • It makes it clearer for those who don't know the defaults, as per your question (I've never liked this argument, personally, but I figured it's worth mentioning)
  • It gives an impression that you've deliberately decided to make it private, rather than just gone with the defaults.

As for your last part:

Moreover is there a case where writing "private" (alone) will change the accessibility of the member?

Yes, for making half of a property more restrictive than the other:

// Public getter, public setter
public int Foo { get; set; }

// Public getter, private setter
public int Bar { get; private set; }

I used to go with defaults everywhere I could, but I've been convinced (partly by Eric Lippert) that making it clear that you've thought about it and decided to make something private is a good idea.

Personally I wish there were a way of doing that for sealed / unsealed, too, for type declarations - possibly not even have a default. I suspect that many developers (myself included if I'm not careful) leave classes unsealed just because it's less effort than making them sealed.

纵山崖 2024-12-27 14:57:45

private 增加了视觉混乱。对于那些坚持认为它使事情变得明确的人,我想问:你在数学上也这样做吗?例如:

var answer = a + b / c;

如果 b / c 周围没有多余的括号,您是否会觉得不清楚?

C# 中的规则非常简单:默认情况下,所有内容都尽可能接近私有。因此,如果您需要比默认情况更加可见的内容,请添加一个修饰符。否则,不要在代码中添加不必要的关键字。

private adds visual clutter. To those who insist that it makes things explicit, I would ask: Do you do this with mathematics, too? For instance:

var answer = a + b / c;

Do you find that unclear without redundant parentheses around b / c?

The rule in C# is very simple: By default, everything is as close to private as it can be. So if you need something to be more visible than the default, add a modifier. Otherwise, don't add needless keywords to your code.

仄言 2024-12-27 14:57:45

据我所知,C# 中所有地方都默认使用 private

显式声明私有意味着您知道它是私有的。不仅仅是你认为是这样,因为据你所知,这是默认的。这也意味着其他查看代码的人知道它是什么。

没有“我认为是”、“我很确定是”等等。它只是。每个人都在同一页面上。

我不是 C# 开发人员。如果我必须使用一些未明确声明为私有的代码,我可能会假设它是内部的。

我不喜欢事情被隐式设定。当它们被明确设置时,它永远不会那么清晰。

As far as I know, private is the default everywhere in C#

Explicitly declaring private, means you know it is private. Not just think it is, because as far as you know, it is the default. It also means that someone else who looks at the code knows what it is.

There is no "I think it is", "I'm pretty sure it is", etc. It just is. And everyone is on the same page.

I am not a C# developer. If I had to work with some code that wasn't explicitly declared private, I would probably assume it was internal.

I dislike when things are implicitly set. It's never as clear as when they are explicitly set.

待"谢繁草 2024-12-27 14:57:45

可读性 - 并非每个人都知道private 是默认行为。

意图 - 明确表明您已明确声明属性私有(无论出于何种原因)。

Readability - Not everyone may know that private is the default behaviour.

Intent - Gives a clear indication that you have specifically declared the property private (for whatever reason).

哆兒滾 2024-12-27 14:57:45

可读性和意图的表达是我能想到的两个重要原因。

Readability, demonstration of intent are two great reasons I can think of.

温柔戏命师 2024-12-27 14:57:45

很多人(像我这样的人!)经常使用几种不同的语言进行编程。明确地处理这些事情可以让我不需要记住我编程所用的所有语言的所有神秘细节。

A lot of people (people like me!) regularly program in a handful of different languages. Being explicit with things like these prevents me from needing to remember all the arcane details of all the languages I program in.

回忆凄美了谁 2024-12-27 14:57:45

显式指定可见性的一个很好的理由是,您不必考虑所处上下文的默认设置。

另一个很好的理由是因为 FxCop 告诉你这样做。

One good reason for explicitly specifying the visibility is so that you don't have to think about what is the default for the context you are in.

Another good reason is because FxCop tells you to do it.

一城柳絮吹成雪 2024-12-27 14:57:45

我想说的是为了与班级其他部分的可读性保持一致。

I'd say for consistency with the readability of the scope of the rest of the class.

池木 2024-12-27 14:57:45

我喜欢明确添加 private 关键字,以便我知道它是私有的。另外,private 并不总是所有地方的默认访问说明符。此外,您可以在属性中使用private set来有效地创建一个只能由声明它的类设置的只读属性。

I like to explicitly add the private keyword so that I know it's private. Plus, private isn't always the default access specifier everywhere. Also, you can use private set in properties to effectively make a read-only property that can only be set by the class it was declared in.

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