C#字段命名指南? [英] C# Field Naming Guidelines?

查看:158
本文介绍了C#字段命名指南?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要工作的一个位C#code的拥有我自己的,但我想确保我遵循最广泛接受的命名约定柜面我希望把其他开发商,释放我的code或出售我的code。眼下,因为他们似乎是最为广泛接受我下面,微软已设定的命名约定。有一件事他们不提,虽然被命名为私人领域。在大多数情况下我已经看到他们像保护领域但是,这种present我一个问题,因为参数名称应在驼峰驼峰而得名。看看下面的构造函数,例如:

I am going to be working on a bit of C# code own my own but I want to make sure that I follow the most widely accepted naming conventions incase I want to bring on other developers, release my code, or sell my code. Right now I am following the naming convention that Microsoft has set as they seem to be the most widely accepted. The one thing they don't mention though is naming for private fields. For the most part I have seen them named in camelCase like protected fields however that present me with an issue as parameter names should be in camelCase. Take the following constructor for example:

public GameItem(string baseName, string prefixName, string suffixName)
{
    //initialize code
}

现在如果我使用驼峰私营领域也存在命名冲突,除非我用本,以访问类字段(我认为这是对大多数标准何况意味着更多的打字)。一种解决方案是给该参数的不同的名称,但不使逻辑意义,得到相同的数据2的不同名称。唯一的其他解决方案,我知道这是用C ++常见的编码是给私有成员开头的下划线。是一种常用的C#编码接受?是否有另一种解决这个问题(如仅使用属性(使用PascalCase)接入领域,即使在类本身)?

Now if I use camelCase for the private fields too there is a naming conflict unless I use "this" in order to access the class fields (which I think is against most standards not to mention means more typing). One solution is to give the parameter a different name but that does not make logical sense to give the same data 2 different names. The only other solution that I know of that was common in C++ coding is giving private members an underscore at the beginning. Is that solution commonly accepted with C# coding? Is there another solution to this problem (like only using properties (which use PascalCase) to access fields, even in the class itself)?

推荐答案

按照的微软命名指南。对于现场使用的指南表明它应该驼峰和不是prefixed。需要注意的是,一般的规则是没有preFIX;具体的规则是不preFIX静态和非静态字段之间进行区分。

Follow the Microsoft Naming Guidelines. The guidelines for field usage indicate that it should be camelCase and not be prefixed. Note that the general rule is no prefix; the specific rule is not to prefix to distinguish between static and non-static fields.

不要使用一个preFIX到字段名或静态字段名。具体地,不一个preFIX应用于一个字段名静态和非静态字段之间进行区分。例如,应用G_或S_ preFIX不正确。

Do not apply a prefix to field names or static field names. Specifically, do not apply a prefix to a field name to distinguish between static and nonstatic fields. For example, applying a g_ or s_ prefix is incorrect.

和(从通用命名约定的)

不要使用下划线,连字号,或其他任何非字母数字字符。

Do not use underscores, hyphens, or any other nonalphanumeric characters.

修改:我会注意,文档是不特定方面的私人的字段,但指出的保护的领域应该只驼峰。我想你可以从这个对于私人领域的任何公约是可以接受的推断。当然,公共静态字段从受保护的不同(他们是大写)。我个人的看法是,受保护/私有不在范围十分不同,值得一差异命名约定,特别是因为所有你似乎想要做的就是从参数区分。也就是说,如果你遵循保护领域的指导方针,你必须区别对待在这方面比私有字段,以便从参数相区别。 <罢>我用这个引用类成员当类内进行区分清楚。

EDIT: I will note that the docs are not specific with regard to private fields but indicate that protected fields should be camelCase only. I suppose you could infer from this that any convention for private fields is acceptable. Certainly public static fields differ from protected (they are capitalized). My personal opinion is that protected/private are not sufficiently different in scope to warrant a difference in naming convention, especially as all you seem to want to do is differentiate them from parameters. That is, if you follow the guidelines for protected fields, you'd have to treat them differently in this respect than private fields in order to distinguish them from parameters. I use this when referring to class members within the class to make the distinction clear.

编辑2

我已经通过的我现在的工作使用的惯例,这是preFIX私有实例变量以下划线一般只公开受保护的实例变量与使用PascalCase(通常autoproperties)属性。这不是我个人的preference但它是一个,我已经成为舒适而且很可能会跟随,直到更好的东西走来。

I've adopted the convention used at my current job, which is to prefix private instance variables with an underscore and generally only expose protected instance variables as properties using PascalCase (typically autoproperties). It wasn't my personal preference but it's one that I've become comfortable with and probably will follow until something better comes along.

这篇关于C#字段命名指南?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆