在C#中使用下划线为私有类变量加前缀是好的还是坏的做法 [英] Is it good or bad practice to prefix private class variables with underscore in C#

查看:477
本文介绍了在C#中使用下划线为私有类变量加前缀是好的还是坏的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开发团队中的许多人来自C ++背景,并且在
中使用下划线为私有类变量添加前缀的实践

提高可读性并避免命名冲突使用局部变量

和参数。这已成为我们的标准。


即私人字符串_name;


而不是:


即私人字符串名称;


然而,其中一个较新的人告诉我们这很糟糕

练习。


这是好的,坏的还是没什么大不了的?

Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?

推荐答案

我认为一致命名比命名风格更重要。


我喜欢并在编写C / C ++代码时使用下划线。我不知道为什么,但是我认为
认为下划线是为C / C ++创建的。


输入C#,我认为下划线用法是微不足道的。


" PromisedOyster" < Pr ************ @ hotmail.comwrote in message

news:11 ****************** ****@h48g2000cwc.googlegr oups.com ...
I think consistant naming is more important than naming style.

I like and use underscores when I write C/C++ code. I don''t know why but I
think underscore is created for C/C++ :)

Enter C#, underscore usage is trivial I think.

"PromisedOyster" <Pr************@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...

我们的开发团队中有很多人来自C ++背景,并且在

使用下划线为私有类变量添加前缀的实践

提高可读性并避免命名与局部变量碰撞

和参数。这已成为我们的标准。


即私人字符串_name;


而不是:


即私人字符串名称;


然而,其中一个较新的人告诉我们这很糟糕

练习。


这是好的,坏的还是没什么大不了的?
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?



PromisedOyster写道:
PromisedOyster wrote:

我们的开发团队中有很多来自C ++背景,并且在
中使用下划线为私有类变量添加前缀的实践

提高了可读性和避免使用局部变量命名碰撞

和参数。这已成为我们的标准。


即私人字符串_name;


而不是:


即私人字符串名称;


然而,其中一个较新的人告诉我们这很糟糕

练习。


这是好的,坏的还是没什么大不了的?
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?



没什么大不了的,只要你保持一致。很长一段时间,我在

工作了一家根本没有使用前缀的公司。我现在工作的公司

使用m_作为实例字段,使用g_作为静态字段。当我正在进行个人开发时,我倾向于不添加任何前缀。


方法通常应该足够短,以至于显而易见的地方一个名字

来自IMO,所以前缀不是必需的 - 但是如果它有一个很大的

偏好,那就顺其自然了。


Jon

No big deal, so long as you''re consistent. For a long time I worked at
a company which didn''t use prefixes at all. The company I now work for
use m_ for instance fields and g_ for static fields. When I''m doing
personal development I tend not to put any prefixes on.

Methods should generally be short enough that it''s obvious where a name
comes from, IMO, so prefixes aren''t necessary - but if there''s a big
preference for it, go with the flow.

Jon


嗨Oyster,


正如其他人所说,一致性是关键。对于使用下划线而言,唯一需要注意的是,某些开发系统和

库倾向于使用下划线来标​​记特殊,隐藏等。或

内部功能。


在开头使用下划线可能会使开发人员混淆这些

特殊方法/类/字段/等等。

但在C#中,这不应该是一个问题。


-

问候,


Jani J?rvinen先生

C#MVP

芬兰赫尔辛基
ja *** @ removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Hi Oyster,

as other have said, consistency is the key. The only thing that comes into
mind against using underscores is that some development systems and
libraries tend to use the underscore to mark "special", "hidden" or
"internal" features.

Using an underscore in the beginning might confuse a developer with such
special methods/classes/fields/whatever.

But in C#, this shouldn''t be an issue.

--
Regards,

Mr. Jani J?rvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/


这篇关于在C#中使用下划线为私有类变量加前缀是好的还是坏的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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