zoukankan      html  css  js  c++  java
  • C#编码规范(四)

    31.  总是使用基于0开始的数组。

    32.  在循环中总是显式的初始化引用类型的数组。

    public class MyClass

    {}

    MyClass[] array = new  MyClass[100];

    for(int index = 0; index < array.Length;  index++)

    {

       array[index] = new  MyClass();

    }

    33.  不要提供public 和 protected的成员变量,使用属性代替他们。

    34.  避免在继承中使用new而使用override替换。

    35.  在不是sealed的类中总是将public 和 protected的方法标记成virtual的。

    36.  除非使用interop(COM+ 或其他的dll)代码否则不要使用不安全的代码(unsafe code)。

    37.  避免显示的转换,使用as操作符进行兼容类型的转换。

    Dog dog = new GermanShepherd();

    GermanShepherd shepherd = dog  as  GermanShepherd;

    if (shepherd != null )

    {…}

    38.  当类成员包括委托的时候

    a)  Copy a delegate to a local variable before publishing to avoid concurrency race

    condition. 

    b)  在调用委托之前一定要检查它是否为null

    public class MySource

    {

       public event EventHandler  MyEvent;

       public void FireEvent()

       {

          EventHandler temp = MyEvent;

          if(temp != null )

          {

             temp(this,EventArgs.Empty);

          }

       }

    }  

    39.  不要提供公共的事件成员变量,使用事件访问器替换这些变量。

    public class MySource

    {

       MyDelegate m_SomeEvent ;

       public event MyDelegate SomeEvent

       {

          add

          {

             m_SomeEvent += value;

          }

          remove

          {

             m_SomeEvent -= value;

          }

       }

    }

    40.  使用一个事件帮助类来公布事件的定义。

  • 相关阅读:
    关于listview嵌套listview
    Android ImageView(scaleType属性)
    在服务器端实现数据任意排序算法
    分页拖放排序dragsort
    拖放鼠标实现内容排序
    计算机科学中最重要的32个算法
    cocos2d-x 获取SD卡图片 线程 UI
    cocos2d-x 静态变量 static
    cocos2d-x 启动触摸事件
    cocos2d-x android混编时图片不显示
  • 原文地址:https://www.cnblogs.com/atun/p/2053395.html
Copyright © 2011-2022 走看看