C# 私有方法命名约定

C# 私有方法命名约定,c#,coding-style,private-methods,C#,Coding Style,Private Methods,我在这里调用了“\u Add”的私有方法的命名是否有约定?我不喜欢领先的底线,但这是我的一位队友所建议的 public Vector Add(Vector vector) { // check vector for null, and compare Length to vector.Length return _Add(vector); } public static Vector Add(Vector vector1, Vector vector2) { // ch

我在这里调用了“
\u Add
”的私有方法的命名是否有约定?我不喜欢领先的底线,但这是我的一位队友所建议的

public Vector Add(Vector vector) {
    // check vector for null, and compare Length to vector.Length
    return _Add(vector);
}

public static Vector Add(Vector vector1, Vector vector2) {
    // check parameters for null, and compare Lengths
    Vector returnVector = vector1.Clone()
    return returnVector._Add(vector2);
}

private Vector _Add(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}
公共向量添加(向量){
//检查vector是否为null,并将长度与vector.Length进行比较
返回加(向量);
}
公共静态向量添加(向量向量向量1、向量向量向量2){
//检查参数是否为空,并比较长度
Vector returnVector=vector1.Clone()
返回向量。添加(向量2);
}
专用向量_Add(向量向量){
for(int index=0;index
我从未见过C语言中有任何区分公共方法和私有方法的编码约定。我不建议这样做,因为我看不到它的好处


如果方法名与公共方法冲突,那么是时候变得更具描述性了;如果像您的情况一样,它包含公共方法的实际方法实现,一种约定是将其称为
*Impl
。也就是说,在你的情况下,
AddImpl

我认为在大多数惯例中,在私人事务上有更多的自由。然而,我经常看到这一点:

private Vector AddCore(Vector vector)

但是,我可能会放弃私有add方法,只使用一个:

public static Vector Add(Vector vector1, Vector vector2) 
{
    // check if vector1 is null
    Vector returnVector = vector1.Clone()
    return returnVector.Add(vector2);
}

public Vector Add(Vector vector) 
{
    // check parameters for null, and compare Lengths
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}
公共静态向量添加(向量向量1、向量向量向量2)
{
//检查vector1是否为空
Vector returnVector=vector1.Clone()
返回returnVector.Add(vector2);
}
公共向量添加(向量向量)
{
//检查参数是否为空,并比较长度
for(int index=0;index

也把那些花括号放在右边:-)

私有属性使用前导下划线是很常见的,但我从来没有见过在方法上使用它来做这件事

用方法名称来描述,以创建解释自身的代码,不应该有任何冲突,因为您不应该让两个方法做完全相同的事情,所以您应该没有理由需要一个字符作为方法名称的前缀


有些人在私有字段前面加上“m_”,我没有看到私有方法有任何类似的样式。

就个人而言,对于方法,无论可见性如何,我都有相同的命名约定

以下是我对C#的命名约定:

  • 名称空间、类型、方法、属性:PascalCase
  • 局部变量:camelCase
  • 方法的参数:camelCase
  • 私有字段:_PascalCase带下划线前缀,如果为属性提供支持字段,则仅与带下划线前缀的属性同名
编辑:注意,我对在私有方法中使用前缀名感到内疚。我第一次读到你问题的那一部分时,没有听懂


例如,如果我有7种不同的方法通过DatabaseCommand类执行SQL语句,比如QueryDataTable、QueryEnumerable、
QueryEnumerable
、QueryDataReader等,那么所有这些方法都需要调用相同的私有方法,我倾向于将此方法称为InternalQuery或PrivateQuery。

我会按照我的队友的建议去做,并在团队中形成惯例。但在特定情况下,您似乎可以避免:

public Vector Add(Vector vector) {
    // check vector for null, and compare Length to vector.Length
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

public static Vector Add(Vector vector1, Vector vector2) {
    // check parameters for null, and compare Lengths
    Vector returnVector = vector1.Clone()
    return returnVector.Add(vector2);
}
公共向量添加(向量){
//检查vector是否为null,并将长度与vector.Length进行比较
for(int index=0;index

或者我不应该这么晚才开始…

我通常用这个案例来表示私有方法,而那个案例用于公共方法

private Vector add(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

public Vector Add(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}
专用矢量添加(矢量){
for(int index=0;index
我通常会看到并使用“AddCore”或“InnerAdd”

不幸的是,我遇到过很多这种约定,还有一种倾向,就是在表单上用前导下划线命名控件(例如“_txtFirstname”而不是“txtFirstname”)。这似乎是一种奇怪的bleedover效应,因为MS不再推荐使用前导下划线命名私有变量。或者程序员只是喜欢使用下划线键,我想不出什么原因

不要使用这种惯例,当你的同事坚持这样做时,请他在网上找到推荐这种做法的东西(任何东西)

由于public Add()执行一些检查,而private没有:

private Vector AddUnchecked(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}
private Vector AddUnchecked(向量向量){
for(int index=0;index
我看到的两种常用变体是:

private Vector DoAdd(Vector vector) { ... }

这两个都不是特别令人满意,但它们是我所看到的

我从来没有见过一个惯例,即所有私有方法都应该有前缀——一想到它就让我不寒而栗

处理所有用“^”来装订每个成员的C++开发人员都是不够好的。我以前是说Delphi开发人员,以前用“F”来对每个成员进行前缀。我还在康复中

公共方法:

public void Add()
{
}
this.Add()
私人方法:

private void _Add()
{
}
_Add();
特性:

public int Id {get;set;}
this.Id = 10;
字段:

private bool _isUsed;
_isUsed = false;
局部变量:

bool isUsed;

在这种情况下,EPiServer使用“…Internal”约定,如
AddInternal()

此外,大多数IDE都会自动建议筛选出私有方法或隐藏它们。此外,如果将方法从public更改为private,或者反之亦然,您确实不希望
private bool _isUsed;
_isUsed = false;
bool isUsed;