zoukankan      html  css  js  c++  java
  • 第十讲:C#中的一些设计技巧

    *编码习惯
    1、命名规范,接口用I开头做前缀,异常类使用Exception作为其后缀。
    public class MyClass
    {
     int myNum=0;
     public MyMethod(int refObject){}
    }

    interface IMyInterface{...}

    public class MyEventException{...}

    2、使用有意义的变量名称和名称空间。有返回值的方法GetMyObjectState().要设定值的方法SetMyObjectState().
    3、所有的成员变量都应该声明在顶部,同时使用一个空行来将他们和属性以及方法分开。
    4、总是将大括号放在一个新行上。

    * Equals()
    考虑覆写Equals(object obj)
    .NET提供了默认的实现方法(引用指向同一对象)
    .NET将在比较和查询时使用这个方法。
    实现自己比较的“相等”定义,你需要覆写这个方法。

    *自定义Equals
    如果两个客户姓名相等,我们认为对象是相等的。
    pulbic class BankCustomer
    {
     public string FirstName;
     public string LastName;
     ......
     public override bool Equals(object obj)
     {
      if(obj==null) return false;
      if((obj.GetType().Equals(this.GetType()))==false) return false;
      BankCustomer other;
      other=(BankCustomer) obj;
      return this.FirstName.Equals(other.FirstName) && this.LastName.Equals(other.LastName);
     }
    }

    *GetHashCode()
    如果覆写了Equals,也需要覆写GetHashCode(警告)
    如果两个对象Equals,GHC必须返回相同的值
    根据Equals定义字段来定义GHC
    pulbic class BankCustomer
    {
     public string FirstName;
     public string LastName;
     ......
     public override int GetHashCode()
     {
      rturn this.FirstName.GetHashCode()+this.LastName.GetHashCode();
     }
    }

    *补充知识
    Hashtable
    Hashtable 提供高效的搜索
    基于(key,value)pairs(键/值)对--键必须是唯一的
    可以通搜索key来查找
    using sc=System.Collections;
    sc.Hashtable customers;
    customers=new sc.Hashtable();
    customers.Add(12345,new BanckCustomer());
    customers.Add(67890,nwe BanckCustomer());
    ......
    BankCustomer c;
    c=(BankCustomer) customers[67890];

    foreach(sc.DictionaryEntry hte in customers)
     ((BankCustomer)hte.Value).Balance+=250.00M;


    *IDisposable
    一些对象需要清理:
    System.IO.StreamWriterfile;
    file=new System.IO.StreamWriter("file.txt");
    file.WriteLine(...);
    ......
    file.Dispose();//flush writes & free file handle;

    Dispose()
    类中是否“抓住”资源?
    a)重新设计类
    b)实现IDisposable接口,并提供Dispose & Close 方法

    *ICIoneable
    如果需发实现克隆功能,那么要实现ICloneable接口
    public class BankCustomer:ICloneable
    {
     ......
     public object Clone()
     {
      return new BankCustomer(this.FirstName,this.LastName,this.Balance);
      //相当于:
      return this.MemberwiseClone();//浅表副本
     }
    }

    *String相关知识
    string是不可变的对象。
    字符串连接操作并不更改当前字符串,只是创建并返回新的字符串,速度慢。
    StringBuilder的字符串连接
    频繁进行字符串连接操作时,使用StringBuilder类来改善性能,连接操作越频繁,差别越明显。
    字符串驻留
    public static Intern(String str)
    public static isIntern(String str)

     

  • 相关阅读:
    Tornado-Lesson06-ORM、SQLAlchemy连接数据库、Module和增删改查
    Tornado-Lesson04-模版、模版转义、静态文件的引用
    Tornado-Lesson05-模版继承、函数和类导入、ui_methods和ui_modules
    博弈论
    给图片添加水印
    Apsara Clouder专项技能认证:实现调用API接口
    好用的工具
    书写是为了更好的思考
    使用与破解ExcelVBA密码
    一款可以直接下载浏览器sources资源的Chrome插件
  • 原文地址:https://www.cnblogs.com/iceberg2008/p/1404219.html
Copyright © 2011-2022 走看看