effective c sharp 印象

今天在 www.cnblogs.com 看到了这么一篇文章《[你必须知道的.NET] 第六回:深入浅出关键字---basethis》【AndTao】的文章,写的不错。

最后发现基本上是对《Effective C#》的翻译重新组织了一下,不过加入了作者的理解,不错的想法。自己也想看看,搜索了一下,总结一下:

(一下资料来源于 CodeProject.com
http://www.codeproject.com/csharp/effectivecspart1.asp

o        Item1 - Prefer the Length property when checking string size. [Performance]

o        Item2 - Prefer StringBuilder over string concatenation. [Performance]

o        Item3 - Avoid Boxing and UnBoxing as much as possible. [Performance]

o        Item4 - Prefer String.Equal method over == operator. [Performance]

o        Item5 - Use Native Image Generator (Ngen.exe) in case of long and heavy initialization. [Performance]

o        Item6 - Prefer 'for' over 'foreach'. [Performance]

o        Item7 - Prefer the ‘as’ operator over direct type casting. [Usage]

o        Item8 - Use the 'checked' keyword to avoid overflow. [Usage]

o        Item9 - Use the 'is' operator before casting. [Usage]

o        Item10 - Use Explicit interface to 'hide' the implementation of an interface. [Usage]

o        Item11 - Use @ to ease the work with literal paths. [Usage]

o        Item12 - Make your API assembly CLS compliant. [Usage]

o        Item13 - Define destructor and implement IDisposable interface for classes that use native resources directly. [Garbage Collection]

o        Item14 - Avoid the use of GC.Collect [Garbage Collection]

o        Item15 - Use StructLayout attribute, for classes and structs, when using COM Interop. [COM Interop]

 由于是E文的,好久没有翻译了,所以兴趣来了就像解释、总结一下:

l          条款1:建议使用Length 属性检查字符串的大小【性能】;

l          条款2:建议使用 StringBuilder 替换 String 进行字符连接【性能】;

l          条款3:尽可能的避免使用 装箱 拆箱 操作【性能】;

l          条款4:推荐使用 String.Equals 方法替代 == 操作【性能】;

l          条款5:对于大量资源初始化动作,建议使用本地图片资源生成器( Ngen.exe )进行管理;

l          条款6:建议使用 for 替代 foreach【性能;

l          条款7:推荐使用 as 替换 直接的类型转换【用法】;

l          条款8:使用 Checked 避免溢出【用法】;

l          条款9:在进行类型转换前使用 is 操作【用法】;

l          条款10:使用确切的接口来隐藏接口的实现【用法】;

l          条款11:应用 @ 来简化字符路径【用法】;

l          条款12:令API程序集符合CLS【用法】;

l          条款13:使用析构函数和继承IDisposable 接口来直接管理本地资源【用法】;

l          条款14:避免使用 GC.Collect 【垃圾回收器】;

l          条款15:调用COM时可以使用StructLayout属性来规定类和结构体【COM】;

相关资料:

CSDN 上的《Effective C#中文版:改善C#程序的50种方法

 

原文地址:https://www.cnblogs.com/AloneSword/p/2237561.html