请教为什么很多书籍或示例代码里变量名前面都带一个下划线?

anantnt203120 2004-06-23 09:26:46
请教为什么很多书籍或示例代码里变量名前面都带一个下划线?
百思不得其解。
...全文
309 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
anantnt203120 2004-06-29
  • 打赏
  • 举报
回复

命名都该有规范啊,这对日后的维护,程序的整洁也是有帮助的啊,写程序也是像作画啊!
wnlovezxm 2004-06-26
  • 打赏
  • 举报
回复
本来就无所谓啊,只要组内统一
anantnt203120 2004-06-26
  • 打赏
  • 举报
回复
这怎么能无所谓呢
tanjingbool 2004-06-26
  • 打赏
  • 举报
回复
习惯而已
YanZhen 2004-06-25
  • 打赏
  • 举报
回复
无所谓,只要自己喜欢就行。最好是别人能够看懂。
anantnt203120 2004-06-24
  • 打赏
  • 举报
回复
有三种命名法:
Pascal Casing – Capitalize the first letter of every word, e.g. MyClassName.

Camel Casing – Capitalize the first letter of every word except the first word, e.g. myVariableName.

Hungarian Notation – Hungarian notation involves including a prefix of the data type as well as a prefix to denote the scope of the variable, e.g. mstrMyString (m stands for “member variable/field in a class” and “str” indicates that the type is a string).
anantnt203120 2004-06-24
  • 打赏
  • 举报
回复
下划线的问题已经解决,我这有一篇微软的命名规范,请大家也参考一下:
Microsoft .NET Coding Standards

1. Overview

This document applies to all .NET compliant languages since any .NET language will utilize the same class library, have the same responsibilities for exception handling and threading, etc. This document outlines the coding convention that should be used, and high lights some key design guidelines as defined by Microsoft. Please refer to the Microsoft Design Guidelines for Class Library Developers [1] for a comprehensive guide on how to write reusable, robust, and scalable components.

2. Coding Conventions

Applying coding conventions in .NET involves the three classical naming schemas: Pascal Casing, Camel Casing, and Hungarian Notation.

Pascal Casing – Capitalize the first letter of every word, e.g. MyClassName.

Camel Casing – Capitalize the first letter of every word except the first word, e.g. myVariableName.

Hungarian Notation – Hungarian notation involves including a prefix of the data type as well as a prefix to denote the scope of the variable, e.g. mstrMyString (m stands for “member variable/field in a class” and “str” indicates that the type is a string).

2.1 Namespace

Pascal casing used for all namespaces. Namespaces that contain functionality that can be reused in other projects should be prefixed with OpenCourse, for example, the namespace OpenCourse.Web.CustomControls.TabControl could be used to describe a custom ASP.NET control named “TabControl.”

2.2 Class

Pascal casing is used for all class names. Class names should be descriptive and abbreviations should be avoided. The underscore character should be avoided as well.

2.3 Interface

Pascal casing is used for all interfaces, and all interfaces shall be prefixed with capital letter “I” to denote that it is an interface. Abbreviations and use of the underscore character should be avoided.

2.4 Method

Pascal casing is used for all methods. It is recommended to use a verb or a verb phrase to describe a method since a method inevitable performs some functionality on the object. Abbreviations and use of the underscore character should be avoided.

2.5 Parameter

Parameter names should be descriptive and use Camel casing.

2.6 Property

Property should be descriptive and use Pascal casing.

2.7 Field

Field names (data members that are members of a class) should use Hungarian notation and use the prefix “m” to denote that the field is a variable that is a member of a class. Example:

VB.NET
Class MyClass

Private mstrMyStringValue As String

End Class

C#

Class MyClass
{
private string mstrMyStringValue;
}


2.8 Event Handlers

An event is a notification sent by an object and this notification can be detected and maybe handled. A verb or gerund is used in naming event handlers. The event name should be suffixed with 'EventHandler'. By convention the calling object should be passed as a parameter along with a parameter for the state of the event.

VB.NET
Public Delegate Sub ScreenEventHandler(sender As Object, e As
ScreenEventArgs)

C#
public delegate void ScreenEventHandler(object sender, ScreenEventArgs e);

2.9 Commenting

At the very minimum, comments must be provided in the .NET XML comment format for all classes, methods, properties, and fields.

3. Design Guidelines

Please refer to the Microsoft Design Guidelines for Class Library Developers [1] for a comprehensive set of design guidelines.

3.1 Exception handling

- Refrain from directly throwing the standard ApplicationException or Exception exceptions. Instead, create a set of exception classes that derive from ApplicationException and use them to throw exceptions generated in the class library. The more specific exceptions, like FileNotFoundException, can be thrown if the correctly convey the type of error.

- Do not use exceptions for normal flow of control. For very common error situations, return null instead of throwing an exception.

- If invalid parameters are passed to a method, always throw an InvalidArgumentException.

- Use localized strings to describe the error.

- If an exception is thrown, clean up what has already been performed in the function. The caller who catches the exception will assume that since an exception was thrown, no work was performed.

3.2 Database Access (ADO.NET)

- Use the built-in .NET connection pooling features. Open a connection for every call, and remember to close the connection. Use a try-finally pattern to ensure that the connection is always closed regardless of if an exception was thrown or not.

- Avoid using the automatic update/delete/insert functionality of the DataSet and CommandBuilder classes for anything but prototype code.

- Use care when using any of the Reader classes (SqlDataReader, OleDbDataReader, OdbcDataReader) since the reader instance will stay open on the connection until explicitly closed.


4. References

[1] Microsoft Corporation. Design Guidelines for Class Library Developers. .NET Framework General Reference. 2003. http://msdn.microsoft.com/library/default.asp?url=/library/en- us/cpgenref/html/cpconnetframeworkdesignguidelines.asp.

haiwangstar 2004-06-24
  • 打赏
  • 举报
回复
c#中是不使用匈牙利命名法的.你看一下.NET的类库命名就知道了...C#中使用两种命名法.PASCAL,和Camel命名法.C#中唯一使用匈牙利命名法的是接口.如IList...
我本人就十分不喜欢匈牙利命名法.
anantnt203120 2004-06-24
  • 打赏
  • 举报
回复
到底谁说的对?
peterli1976 2004-06-23
  • 打赏
  • 举报
回复
那是c系列风格的变量定义,习惯而已.
c#.net一般用NameTomPeter这样单词首位大写的记法表示
demonfox 2004-06-23
  • 打赏
  • 举报
回复
naming convention, nothing serious as long as you like it
sea026 2004-06-23
  • 打赏
  • 举报
回复
类的成员变量一般这样,用于区别成员函数的参数。绝对是经验啊,推荐!
---------------------------------------------------------------------

误导。
是在MSDN上,还是《C#高级编程》上有说明,建议私有变量名称为匈牙利命名。
小贵子88 2004-06-23
  • 打赏
  • 举报
回复
类的成员变量一般这样,用于区别成员函数的参数。绝对是经验啊,推荐!
anantnt203120 2004-06-23
  • 打赏
  • 举报
回复
哦~~~

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧