在C#中为类字段命名首选项? [重复]

时间:2023-01-28 18:02:38

I have seen several naming conventions used for fields in C#. They are:

我已经看到了几个用于C#中字段的命名约定。他们是:

Underscore

public class Foo
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

This

public class Foo
{
    private string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
}

Member Prefix

public class Foo
{
    private string m_name;
    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }
}

Which do you prefer? Is there a different way you prefer to do it? Just curious to know what others feel is a best practice.

你喜欢哪个?你喜欢这种方式吗?只是好奇知道别人的感受是最好的做法。

4 个解决方案

#1


I usually use:

我通常使用:

public class Foo
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

just removed the this. since it's rather redundant

刚刚删除了这个。因为它相当多余

#2


The best practice is to pick one, and be consistent.

最佳做法是选择一个,并保持一致。

I prefer "", personally. "m" has always seemed to me to make the "memberness" seem to be something special, as though it were for the benefit of developers coming from a background where no "members" exist. Sort of the same with "this.". I'd prefer just "name" instead.

我个人更喜欢“”。在我看来,“m”似乎总是让“成员”看起来很特别,好像它是为了开发人员的利益而来自没有“成员”存在的背景。用“this”排序相同。我更喜欢“名字”而不是。

#3


1) This is a purely stylistic/subjective question, so one of these is, in general, as good as another. (I qualify that because the last time I answered a question like this, even with a nearly identical qualification, I had all kinds of people voting me down and telling me I was wrong.)

1)这是一个纯粹的风格/主观问题,因此其中一个问题通常与另一个问题一样好。 (我有资格认为,因为上次我回答这样的问题,即使资格几乎相同,我也让各种各样的人投票给我,告诉我我错了。)

2) I use the this method. It's the default technique used by StyleCop, and I don't have any other source analysis tool available to me at the moment. It's perfectly acceptable.

2)我使用这种方法。这是StyleCop使用的默认技术,目前我没有任何其他源分析工具可用。这是完全可以接受的。

#4


I always use what you call the this style, though I won't religiously use this.

我总是使用你称之为这种风格的东西,尽管我不会虔诚地使用它。

#1


I usually use:

我通常使用:

public class Foo
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

just removed the this. since it's rather redundant

刚刚删除了这个。因为它相当多余

#2


The best practice is to pick one, and be consistent.

最佳做法是选择一个,并保持一致。

I prefer "", personally. "m" has always seemed to me to make the "memberness" seem to be something special, as though it were for the benefit of developers coming from a background where no "members" exist. Sort of the same with "this.". I'd prefer just "name" instead.

我个人更喜欢“”。在我看来,“m”似乎总是让“成员”看起来很特别,好像它是为了开发人员的利益而来自没有“成员”存在的背景。用“this”排序相同。我更喜欢“名字”而不是。

#3


1) This is a purely stylistic/subjective question, so one of these is, in general, as good as another. (I qualify that because the last time I answered a question like this, even with a nearly identical qualification, I had all kinds of people voting me down and telling me I was wrong.)

1)这是一个纯粹的风格/主观问题,因此其中一个问题通常与另一个问题一样好。 (我有资格认为,因为上次我回答这样的问题,即使资格几乎相同,我也让各种各样的人投票给我,告诉我我错了。)

2) I use the this method. It's the default technique used by StyleCop, and I don't have any other source analysis tool available to me at the moment. It's perfectly acceptable.

2)我使用这种方法。这是StyleCop使用的默认技术,目前我没有任何其他源分析工具可用。这是完全可以接受的。

#4


I always use what you call the this style, though I won't religiously use this.

我总是使用你称之为这种风格的东西,尽管我不会虔诚地使用它。