C# 风格的问题,ctor 变量名称&类属性名称

发布于 2024-09-14 19:04:21 字数 341 浏览 8 评论 0原文

尝试按照书本做所有事情,这是正确的吗?:

public class JollyHockey
{
    public string AnotherCoolProperty { get { return anotherCoolProperty; } }

    string anotherCoolProperty;

    public JollyHockey(string anotherCoolProperty)
    {
         this.anotherCoolProperty = anotherCoolProperty;
    }
}

或者有些人使用下划线表示私有类变量?

Trying to do everything by the book, is this correct?:

public class JollyHockey
{
    public string AnotherCoolProperty { get { return anotherCoolProperty; } }

    string anotherCoolProperty;

    public JollyHockey(string anotherCoolProperty)
    {
         this.anotherCoolProperty = anotherCoolProperty;
    }
}

Or do some people use underscores for the private class variables?

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

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

发布评论

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

评论(4

话少心凉 2024-09-21 19:04:21

有些人(包括我自己)在私有类变量前加上下划线(只是作为在何处使用什么的视觉指示)。

这主要是个人(或团队)级别的风格考虑,您可以使用您想要的(或您的团队标准化的)。

只要确保你保持一致即可!

对于它的价值,您还可以使用自动属性作为示例:

public class JollyHockey
{
    public string AnotherCoolProperty { get; private set; }
    public JollyHockey(string anotherCoolProperty)
    {
        AnotherCoolProperty = anotherCoolProperty;
    }
}

Some people (including myself) prefix private class variables with an underscore (simply as a visual indication of what is being used where).

This is mainly a personal (or team) level style consideration and you can use what you want (or what your team has standardized on).

Just be sure you're consistent!

For what it's worth, you could also use auto-properties for your example:

public class JollyHockey
{
    public string AnotherCoolProperty { get; private set; }
    public JollyHockey(string anotherCoolProperty)
    {
        AnotherCoolProperty = anotherCoolProperty;
    }
}
阳光下慵懒的猫 2024-09-21 19:04:21

或者你可以这样做:

public class JollyHockey
{
    public string AnotherCoolProperty { get; private set; }

    public JollyHockey(string anotherCoolProperty)
    {
        this.AnotherCoolProperty = anotherCoolProperty;
    }
}

Or you can do this:

public class JollyHockey
{
    public string AnotherCoolProperty { get; private set; }

    public JollyHockey(string anotherCoolProperty)
    {
        this.AnotherCoolProperty = anotherCoolProperty;
    }
}
是伱的 2024-09-21 19:04:21

我相信您的示例符合 MS 编码指南。不过,我不喜欢这样,这是你们团队可以同意的。

我不喜欢它的原因是因为底层字段名称经常与方法参数冲突。我使用下划线来表明它们是私有变量。

I believe that your example agrees with the MS coding guidelines. However, I don't like it, and this is something that can be agreed upon by your team.

The reason I don't like it is because the underlying field name often conflicts with method parameters. I use an underscore to make it clear that they are private variables.

原野 2024-09-21 19:04:21

当函数参数与私有字段同名时,

我通常在参数前加上下划线

我认为这是有意义的

ReSharper 的东西由 fletcher 是一个好主意

When a function parameter has the same name as a private field,

I usually prefix the parameter with an underscore

I think it makes sense

the ReSharper thing By fletcher is a good idea

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