青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

c# class 實現 () (很好)

Introduction

While implementing my first projects using C# I found out that there were several issues to take into account if I wanted my classes to behave correctly and make good friends with .NET. This list is more about the "hows" and the "whats" and not the "whys" and while it is in no way complete, it contains the guidelines that I currently follow. Ah, and I almost forgot... it's guaranteed to be incomplete!

The Guidelines

  1. ImplementIComparable.CompareTo() if ordering of objects makes sense. Also implement operators <, <=, > and >= in terms of IComparable.CompareTo().
    int IComparable.CompareTo(object obj)
    { 
        return m_data - ((MyType)obj).m_data; 
    } 
    
    publicstaticbooloperator<(MyType lhs, MyType rhs)
    { 
        return ((IComparable)lhs).CompareTo(rhs) < 0;
    }
    
    publicstaticbooloperator<=(MyType lhs, MyType rhs)
    { 
        return ((IComparable)lhs).CompareTo(rhs) <= 0;
    }
    
    publicstaticbooloperator>(MyType lhs, MyType rhs)
    { 
        return ((IComparable)lhs).CompareTo(rhs) > 0;
    }
    
    publicstaticbooloperator>=(MyType lhs, MyType rhs)
    { 
        return ((IComparable)lhs).CompareTo(rhs) >= 0;
    }
    						
  2. Implement conversion functions only if they actually make sense. Make conversions explicit if the conversion might fail (throw an exception) or if the conversion shouldn’t be abused and it’s only necessary for low level code. Only make conversions implicit if it makes the code clear and easier to use and the conversion cannot fail.
    private MyType(int data)
    { 
        m_data = data;
    }
    
    publicstaticexplicitoperatorMyType(int from)
    { 
        returnnew MyType(from);
    }
    
    publicstaticimplicitoperatorint(MyType from)
    { 
        return from.m_data;
    }
    						
  3. Always implement Object.ToString() to return a significant textual representation.
    publicoverridestring ToString()
    { 
        returnstring.Format("MyType: {0}", m_data);
    }
    						
  4. Implement Object.GetHashCode() and Object.Equals()if object equality makes sense. If two objects are equal (Object.Equals() returns true) they should return the same hash code and this value should be immutable during the whole lifecycle of the object. The primary key is usually a good hash code for database objects.
    For reference types implement operators == and != in terms of Object.Equals().
    publicoverrideintGetHashCode()
    { 
        return m_data.GetHashCode();
    }
    
    publicoverrideboolEquals(object obj)
    { 
        
    // Call base.Equals() only if this class derives from a 
    // class that overrides Equals()
    if(!base.Equals(obj)) returnfalse; if(obj == null) returnfalse; // Make sure the cast that follows won't failif(this.GetType() != obj.GetType()) returnfalse; // Call this if m_data is a value type MyType rhs = (MyType) obj; return m_data.Equals(rhs.m_data); // Call this if m_data is a reference type//return Object.Equals(m_data, rhs.m_data); } publicstaticbooloperator==(MyType lhs, MyType rhs) { if(lhs == null) returnfalse; return lhs.Equals(rhs); } publicstaticbooloperator!=(MyType lhs, MyType rhs) { return !(lhs == rhs); }
  5. For value types, implement Object.Equals() in terms of a type-safe version of Equals() to avoid unnecessary boxing and unboxing.
    publicoverrideintGetHashCode()
    { 
        return m_data.GetHashCode();
    }
    
    publicoverrideboolEquals(object obj)
    { 
        
    								
    if(!(obj is MyType))
            returnfalse;
    
        returnthis.Equals((MyType) obj);
    }
    
    publicboolEquals(MyType rhs)
    {
        
    								
    // Call this if m_data is a value type  
    return m_data.Equals(rhs.m_data); // Call this if m_data is a reference type
    //
    return Object.Equals(m_data, rhs.m_data); } publicstaticbooloperator==(MyType lhs, MyType rhs) { return lhs.Equals(rhs); } publicstaticbooloperator!=(MyType lhs, MyType rhs) { return !lhs.Equals(rhs); }
  6. Enumerations that represent bit masks should have the [Flags] attribute.

  7. All classes and public members should be documented using XML comments. Private members should be documented using normal comments. XML comments should at least include <summary>, <param> and <returns> elements.

  8. If a class is just meant to be a "container" for static methods (has no state), it should declare a private parameter-less constructor so it can’t be instantiated.

  9. All classes should be CLS compliant. Add an [assembly:CLSCompliant(true)] attribute in the AssemblyInfo.cs file. If it is convenient to add a non-CLS compliant public member add a [CLSCompliant(false)] attribute to it.

  10. All implementation details should be declared as private members. If other classes in the same assembly need access, then declare them as internal members. Try to expose as little as possible without sacrificing usability.

  11. strings are immutable objects and always create a new copy for all the mutating operations, which makes it inefficient for assembling strings. StringBuilder is a better choice for this task.

  12. object.MemberWiseClone() provides shallow copying. Implement ICloneable to provide deep copy for classes. ICloneable.Clone() is usually implemented in terms of a copy constructor.
    publicMyType(MyType rhs)
    { 
        m_data = rhs.m_data;
    } 
    
    publicobjectClone()
    { 
        returnnew MyType(this); //調用拷貝構造函數
    }
    						
  13. If a class represents a collection of objects implement one or more indexers. Indexers are a special kind of property so they can be read-only, write-only or read-write.
    publicobjectthis[int index]
    { 
       get { return m_data[index]; }set { m_data[index] = value; }
    }
    						
  14. For a "collection" class to be used in a foreach loop it must implementIEnumerable. IEnumerable.GetEnumerator() returns a class the implements IEnumerator.
    publicclass MyCollection: IEnumerable
    { 
        public IEnumerator GetEnumerator()
        { 
            returnnewMyCollectionEnumerator(this);
        }
    }
    						
  15. The IEnumerator for a "collection" class is usually implemented in a private class. An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, Current will return the element that it is set to, even if the enumerator is already invalidated.
    privateclass MyCollectionEnumerator: IEnumerator
    { 
        public MyCollectionEnumerator(MyCollection col)
        { 
            m_col = col;
            m_lastChanged = col.LastChanged;
        } 
    
        publicboolMoveNext()
        { 
            if(m_lastChanged != m_col.LastChanged)
                thrownew InvalidOperationException();
    
            if(++m_index >= m_col.Data.Count)
                returnfalse; 
    
            returntrue;           
        }
    
        publicvoidReset()
        { 
            if(m_lastChanged != m_col.LastChanged)
                thrownew InvalidOperationException();
    
            m_index = -1;
        }
    
        publicobjectCurrent
        { 
            get { return m_col.Data[m_index]; } 
        } 
    }
    						
  16. There is no deterministic destruction in C# (gasp!), which means that the Garbage Collector will eventually destroy the unused objects. When this scenario is not ok, implement IDisposable...
    publicclass MyClass
    {
        ...
        public ~MyClass()
        {
            Dispose(false);
        }
    
        publicvoid Dispose()
        {
            
    								
    	 Dispose(true);
            GC.SuppressFinalize(this);// Finalization is now unnecessary
        }
       
        protectedvirtualvoid Dispose(bool disposing)
        {
            
    								
    	if(!m_disposed)
            {
                if(disposing)
                {
                    // Dispose managed resources
                }
             
                // Dispose unmanaged resources
            }
          
            m_disposed = true;
        }
       
        privatebool m_disposed = false;
    }
    
    						
    And use the using statement to dispose resources as soon the object goes out of scope
    using(MyClass c = new MyClass())
    {
        ...
    } // The compiler will call Dispose() on c here
  17. There is no way to avoid exceptions handling in .NET. There are several strategies when dealing with exceptions:

    • Catch the exception and absorb it
      try
      {
          ...
      }
      catch(Exception ex)
      {
          Console.WriteLine("Opps! Something failed: {0}", ex.Message);
      }
      										
    • Ignore the exception and let the caller deal with it if there's no reasonable thing to do.
      publicvoid DivByZero()
      {
          int x = 1 / 0; // Our caller better be ready to deal with this!
      }
      										
    • Catch the exception, cleanup and re-throw
      try
      {
          ...
      }
      catch(Exception ex)
      {
          // do some cleanupthrow;
      }
      										
    • Catch the exception, add information and re-throw
      try
      {
          ...
      }
      catch(Exception ex)
      {
          thrownew Exception("Something really bad happened!", ex);
      }
      										
  18. When catching exceptions, always try to catch the most specific type that you can handle.
    try
    {
        int i = 1 / 0;
    }
    catch(DivideByZeroException ex) // Instead of catch(Exception ex)
    {
        ...
    }
    
    						
  19. If you need to define your own exceptions, derive from System.ApplicationException, not from System.Exception.

  20. Microsoft's FxCop design diagnostic tool is your friend. Use it regularly to validate your assemblies.

  21. The definite guide for .NET class library designers is .NET Framework Design Guidelines .

posted on 2006-03-15 17:02 夢在天涯 閱讀(912) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1818821
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              国产精品午夜在线| 国产亚洲精品bv在线观看| 亚洲高清不卡一区| 欧美成人精精品一区二区频| 久久亚洲视频| 亚洲精品国产视频| 亚洲美女淫视频| 国产精品免费一区二区三区在线观看| 亚洲欧美一区二区精品久久久| 亚洲影视中文字幕| 韩国精品久久久999| 欧美阿v一级看视频| 欧美成人免费观看| 亚洲综合欧美| 久久国产精品亚洲77777| 亚洲国产成人av在线| 亚洲激情视频在线| 欧美色欧美亚洲高清在线视频| 欧美综合二区| 免费久久99精品国产自| 亚洲一区在线免费| 欧美主播一区二区三区| 日韩视频在线你懂得| 亚洲自拍偷拍色片视频| 亚洲国产精品悠悠久久琪琪| 99v久久综合狠狠综合久久| 国产欧美视频一区二区三区| 欧美福利视频| 国产嫩草一区二区三区在线观看| 久久亚洲国产成人| 欧美日韩亚洲三区| 老**午夜毛片一区二区三区| 欧美日韩亚洲综合| 欧美国产激情| 国产伦精品一区二区三区照片91 | 亚洲高清中文字幕| 亚洲精选在线| 亚洲第一二三四五区| 亚洲午夜激情| 亚洲精品久久久久中文字幕欢迎你| 亚洲午夜成aⅴ人片| 亚洲精选国产| 老司机一区二区| 欧美影院成年免费版| 欧美日韩免费网站| 欧美高清视频| 狠狠狠色丁香婷婷综合久久五月| 亚洲精品影视| 91久久精品美女高潮| 久久国内精品视频| 欧美在线一区二区| 欧美午夜精品久久久| 亚洲欧洲精品一区二区三区不卡| 国产一区二区黄| 亚洲综合国产| 欧美一级在线视频| 欧美午夜欧美| 日韩亚洲欧美成人一区| 日韩亚洲欧美成人| 欧美黄色aaaa| 最新国产成人av网站网址麻豆| 伊人久久av导航| 欧美一区二区视频在线| 午夜一级久久| 国产精品理论片在线观看| 一区二区免费看| 亚洲午夜精品久久久久久浪潮| 欧美精品日韩www.p站| 亚洲精品一区二区三区四区高清| 亚洲欧洲日本一区二区三区| 欧美14一18处毛片| 亚洲欧洲一区二区天堂久久| 99re这里只有精品6| 欧美日韩免费观看中文| 一区二区三区成人| 欧美一区亚洲一区| 精品成人一区二区| 久久久噜噜噜久噜久久| 亚洲成人在线视频播放| 亚洲高清不卡av| 欧美成人午夜免费视在线看片 | 99精品欧美一区二区三区| 免费观看成人鲁鲁鲁鲁鲁视频 | 欧美一区1区三区3区公司| 国产精品露脸自拍| 欧美在线观看一区二区| 美女日韩欧美| 99国产精品99久久久久久| 欧美日韩国产探花| 亚洲素人在线| 久久岛国电影| 亚洲国产成人高清精品| 欧美日韩精品不卡| 午夜免费电影一区在线观看| 农夫在线精品视频免费观看| 亚洲美女区一区| 国产欧美婷婷中文| 欧美国产精品va在线观看| 一区二区三区鲁丝不卡| 久久嫩草精品久久久精品| 日韩一级黄色大片| 国产一区二区三区四区| 欧美日韩不卡| 久久精品九九| 一本久道综合久久精品| 久久综合精品国产一区二区三区| 99国产精品久久久久久久成人热 | 久久久夜夜夜| av成人老司机| 免费成人黄色av| 午夜视频一区| 亚洲国产精品久久久久婷婷884 | 国产伦精品一区二区三区| 免费视频久久| 久久精品91久久香蕉加勒比 | 久久精品成人欧美大片古装| 亚洲欧洲综合另类| 国模吧视频一区| 国产精品视频九色porn| 欧美成人精品激情在线观看| 欧美亚洲一级| 亚洲综合视频1区| 亚洲精品视频在线| 欧美va天堂在线| 久久九九久精品国产免费直播| 正在播放欧美一区| 亚洲高清一区二| 国自产拍偷拍福利精品免费一| 国产精品久久久久久久久久久久久久| 欧美成人资源网| 快射av在线播放一区| 久久www成人_看片免费不卡| 亚洲一级片在线观看| 中文在线不卡| 亚洲系列中文字幕| 日韩天堂av| 亚洲伦理在线免费看| 亚洲激情欧美| 亚洲人成网站影音先锋播放| 亚洲国产导航| 亚洲人在线视频| 亚洲国产一区二区在线| 亚洲福利在线视频| 亚洲国产精品免费| 亚洲国产婷婷香蕉久久久久久| 欧美大片在线观看一区二区| 久久精品国产亚洲aⅴ| 久久成人精品无人区| 午夜免费日韩视频| 欧美在线免费一级片| 久久福利影视| 麻豆久久久9性大片| 久久这里只有精品视频首页| 久久久久国产精品人| 久久夜色精品国产欧美乱| 久久久精品网| 你懂的亚洲视频| 亚洲黄色免费网站| 99国产精品自拍| 亚洲免费在线精品一区| 欧美一区二区三区免费视| 欧美在线关看| 免费观看成人| 国产精品豆花视频| 国产揄拍国内精品对白| 亚洲激情成人在线| 一区二区三区日韩欧美| 午夜精彩国产免费不卡不顿大片| 欧美尤物巨大精品爽| 免播放器亚洲一区| 亚洲精品日韩综合观看成人91| 一区二区三区精品| 午夜精品婷婷| 欧美国产免费| 国产精品一区二区黑丝| 在线看日韩欧美| 亚洲欧美不卡| 欧美电影在线免费观看网站| 一本色道久久综合亚洲精品不卡 | 欧美日韩国产首页在线观看| 国产精品jvid在线观看蜜臀| 激情五月婷婷综合| 日韩亚洲欧美在线观看| 久久精品91久久久久久再现| 亚洲欧洲一区二区在线观看| 亚洲欧美日韩一区二区在线| 欧美成人日本| 国产视频在线观看一区二区三区| 亚洲成人在线观看视频| 一区二区三区视频在线看| 久久高清国产| 亚洲美女啪啪| 久久蜜桃精品| 国产精品久久久久久久久借妻| 在线播放日韩专区| 久久成人精品无人区| 一片黄亚洲嫩模| 欧美福利一区| 黄色亚洲大片免费在线观看| 亚洲国产婷婷香蕉久久久久久99|