最佳c#编码做法 [英] Best c# Coding Practices

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

问题描述

大家好,

我使用c#已有3个月多了,但我仍然认为我的代码仍不成熟,对初学者来说意味着代码.
我想提高编码水平并增强代码.

我有几个问题,例如:
-何时使用camelCase和PascalCase?
-我应该使用什么前缀?
-如何创建/处理对象?
-这样有助于改善我的编码...

你们有什么想法/建议吗?

预先感谢.

Hi All,

I am using c# for over 3 months now and I still think that my codes are still immature, meaning codes for a beginner.
I want to raise my level of coding and enhance my code.

I have a few questions like:
-when to use camelCase and PascalCase?
-what prefixes should I use?
-how to create/dispose objects?
-and such that will help improve my coding...

Do you guys have any ideas/suggestions.

Thanks in advance.

推荐答案

http://msdn.microsoft.com/en-us/library/ms229042.aspx [ C#编码标准和最佳编程实践 [
http://msdn.microsoft.com/en-us/library/ms229042.aspx[^]
C# Coding Standards and Best Programming Practices[^]


对于堆栈变量,请使用驼峰大小写,否则请使用pascal大小写.有些类型的私人成员也会使用骆驼套.不要使用下划线,太短的名称.有关更多详细信息,请参见Microsoft命名约定.安装FxCop以查看您的命名,请参见 http://en.wikipedia.org/wiki/FxCop [ ^ ].使用常识.名称应具有语义.标签不应称为Label1,应用程序名称不应包含单词"App"或"Application",类名称不应包含单词"Class",等等.

没有前缀.

创建对象的方法很少:用"new"调用构造函数或返回创建的对象的静态工厂方法(用于隐藏构造函数或使用抽象基类的方法创建派生对象).艺术不是创建对象,而是创建构造函数和工厂方法.
您需要并且应该只处置实现接口IDisposable的对象.有两种基本模式:使用对象的构造和层次结构(例如,请参见System.Windows.Forms.Form.Dispose(Boolean) http://msdn.microsoft.com/zh-CN/library/aw58wzka.aspx [ ^ ].有关带有使用"的模式,请参见 http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx [ ^ ].由于您不能依赖删除对象的时刻,因此经验法则是:不要编写析构函数.虽然有例外.

而且这样将有助于改善我的编码",这使问题得以解决.这是终生的经历.

—SA
Use camel case for stack variables, pascal case otherwise. Some use camel case also for private members of a type. Don''t use underscore, too short names. For more detail, see Microsoft naming conventions. Install FxCop to review your naming, see http://en.wikipedia.org/wiki/FxCop[^]. Use common sense. Names should be semantic. A label should not be called Label1, application name should not contain word "App" or "Application", a class name should not contain the word "Class", etc.

No prefixes.

There are few ways of creating objects: calling a constructor with "new" or a static factory method returning created object (used to hide constructor or create a derived object my the method of abstract base class). The art is not creating objects but creating constructors and factory methods.
You need and should dispose only the objects implementing the interface IDisposable. There are two basic patterns: using "using" construct and hierarchy of objects (for example, see System.Windows.Forms.Form.Dispose(Boolean), http://msdn.microsoft.com/en-us/library/aw58wzka.aspx[^]. For the pattern with "using" see http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.71%29.aspx[^], use it whenever possible instead of manually calling Dispose().

If by "disposing" you mean deletion of the object, in .NET the Garbage Collector (GC) takes care of it. Read this: http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx[^]. As you cannot rely on the moment of deletion of the object, the rule of thumb is: do not write destructors. There are exclusions though.

"And such that will help improve my coding" makes the question way to broad. This is lifetime experience.

—SA


这篇关于最佳c#编码做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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