C#编程约定 [英] C# programming conventions

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

问题描述

我偶然发现了 net.tutsplus.com 上的一篇文章> [ ^ ]几周前的日期有关初学者的命名约定.

1.以大写字母开头的变量名称表示它们是一类.
2.在变量名前加下划线表示该变量是私有的或受保护的.
3.所有大写字母都表示一个常量
4.在bool变量前加上"is"(可在其他MS网站或编程博客中找到)
5.还有其他几个我不想列出的内容

我认为这很棒,很显然我是一个初学者...现在,我意识到并不是每个人都遵循规则或准则,并且公司有不同的编码标准,但是...

作为一名新开发人员,并且由于公司中没有人可以启发我.我希望这里有人可以.我对维护某种编码标准非常感兴趣,如果需要的话,外部各方也可以理解.

我注意到在Windows编程中,尤其是使用C#编程时,有时我看到的代码符合上述各项,而其他时候则不符合.

因此...我应该用首字母大写命名所有类,并使用下划线表示私有或受保护的变量吗?即使VS2010不这样做,我是否重命名表单上的所有控件以遵循这种做法?

是否有某种出版物已成为解释该现象的原因和方式的标准指南?

谢谢
Jeff

I stumbled across an article from net.tutsplus.com[^] that was dated a few weeks ago about naming conventions for beginners.

1. Variable names that begin with a capital letter mean that they are a class.
2. Prefixing a variable name with the underscore indicates that the variable is private or protected.
3. ALL CAPS indicate a constant
4. Prefixing a bool variable with "is" (found in anther MS website or programming blog)
5. There are several others that I won''t bother to list

I think this is great and it is obvious that I am a beginner... Now, I realize that not everyone follows the rules or guidelines and companies have different coding standards but...

Being a new developer and since there is no one around in the company that I work to enlighten me. I hope someone here can. I am very interested in maintaining some sort of coding standard that would be understood by outside parties if the need should ever arise.

I notice in windows programming, specifically with C#, that sometimes I see code that adheres to the items above and other times it does not.

So... should I name all of my classes with the first character capitalized and use the underscore to denote a private or protected variable? Do I rename all of the controls on a form to follow this practice even though VS2010 does not do this?

Is there a publication of some sort that has become a standard guideline that explains the why and hows of this?

Thanks
Jeff

推荐答案

Jeff,

看看 StyleCop .我认为您会发现它很有用.

StyleCop分析C#源代码以强制执行一组样式和一致性规则.它可以从Visual Studio内部运行,也可以集成到MSBuild项目中.通过为C#代码强制执行一组通用的样式规则来提供价值.开发人员可以选择执行自己的规则.
Hi Jeff,

Take a look at StyleCop. I think you will find it usefull.

StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. Provides value by enforcing a common set of style rules for C# code. Developers can implement their own rules if they so choose.


事实上,Microsoft已经发布了一套.NET Framework设计指南.您可以在此处在线找到它们: http://msdn.microsoft.com/en-us/library/ms229042 .aspx [^ ]

它们还以Brad Abrams和Krzysztof Cwalina的著作形式存在.可在 Amazon [
As a matter of fact, Microsoft has published a set of design guidelines for the .NET Framework. You can find them online here: http://msdn.microsoft.com/en-us/library/ms229042.aspx[^]

They also exist as a book by Brad Abrams and Krzysztof Cwalina. It''s available on Amazon[^] and most other major book retailers.

One thing to bear in mind is that these are guidelines, not hard and fast rules. Follow whatever standards your company has first. Beyond that, do what makes sense for your situation, especially when it comes to names. Using Pascal-casing for class names and underscores for private member variables is generally a good practice. Renaming stuff auto-generated by Visual Studio is not. You''d be spending a lot of time on something with essentially no benefit.


没有理由和方式,这是为了稍后能够理解而提出的编码标准在不同时间和重用期间,对于不同的公司来说是不同的,这些是由公司标准设定的,其中一些遵循骆驼​​外壳,瓶盖和其他几个标准.如果您可以遵循一个没问题的标准,那么就不必只是为了让生活更轻松就把它当作一成不变的规则.
there is no why and hows, it is the coding standards set forth just to be able to understand at later part of time and during reuse, It is different for different companies and these are set by the company standards some follow camel casing, caps and several other standards. If you could follow one standard that''s ok no need to treat it as a hard and fast rule just to make life easier that''s it.


这篇关于C#编程约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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