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

C++ Programmer's Cookbook

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

c# class 實現(xiàn) () (很好)

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); //調(diào)用拷貝構造函數(shù)
    }
    						
  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 夢在天涯 閱讀(898) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811737
  • 排名 - 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>
              亚洲男人天堂2024| 99伊人成综合| 免费成人av在线看| 亚洲欧洲在线免费| 亚洲精品一区二区三区不| 欧美日韩三级一区二区| 亚洲欧美日韩电影| 欧美专区中文字幕| 亚洲精品免费在线| 亚洲一区二区黄色| 亚洲成色777777女色窝| 亚洲精品日韩精品| 国产麻豆9l精品三级站| 美女日韩在线中文字幕| 欧美国产日韩一区| 欧美一区二区三区在线视频 | 欧美在线播放一区二区| 久久久久久黄| 中日韩美女免费视频网址在线观看| 中国亚洲黄色| 亚洲国产精品一区| 亚洲特色特黄| 亚洲激情图片小说视频| 亚洲天堂网在线观看| …久久精品99久久香蕉国产| 99视频精品在线| 在线观看欧美日韩| 亚洲桃色在线一区| 亚洲人成网站影音先锋播放| 亚洲午夜精品久久| 日韩特黄影片| 久久久之久亚州精品露出| 亚洲自拍高清| 欧美精品亚洲精品| 老妇喷水一区二区三区| 国产精品草莓在线免费观看| 欧美国产日韩亚洲一区| 国产日韩综合一区二区性色av| 亚洲精品麻豆| 在线欧美亚洲| 久久精品成人欧美大片古装| 亚洲欧美电影在线观看| 欧美日产国产成人免费图片| 媚黑女一区二区| 国产欧美日韩一区二区三区在线观看| 亚洲电影免费观看高清完整版在线 | 久久久久久久久久久一区 | 女人香蕉久久**毛片精品| 久久国产主播| 国产精自产拍久久久久久| 91久久国产精品91久久性色| 1024国产精品| 久久久久网站| 久久免费少妇高潮久久精品99| 欧美性片在线观看| 亚洲视频在线视频| 亚洲视频在线一区| 欧美涩涩网站| 一区二区日本视频| 亚洲天堂av综合网| 国产精品v亚洲精品v日韩精品| 亚洲日韩中文字幕在线播放| 亚洲免费成人av电影| 欧美激情久久久久| 亚洲高清视频一区| 一区电影在线观看| 欧美午夜精品理论片a级大开眼界| 日韩视频在线一区二区| 亚洲深夜影院| 国产精品永久免费在线| 午夜在线观看欧美| 免费不卡亚洲欧美| 亚洲精品久久视频| 欧美日韩国产不卡在线看| 亚洲精品视频在线播放| 亚洲一区免费| 国产视频一区二区在线观看| 久久精品国产v日韩v亚洲 | 亚洲一区二区视频| 国产免费观看久久黄| 久久精品一区二区三区中文字幕| 久久综合久久美利坚合众国| 亚洲国产日韩一级| 欧美三区在线| 久久精品国产久精国产爱| 欧美高清不卡| 一区二区三区欧美亚洲| 国产欧美精品在线观看| 久久一区二区精品| 亚洲毛片一区| 久久久久.com| 亚洲精品国精品久久99热一| 国产精品护士白丝一区av| 久久不射网站| 亚洲精品影视| 久久美女艺术照精彩视频福利播放| 亚洲第一中文字幕在线观看| 欧美日韩一区综合| 久久婷婷国产麻豆91天堂| 亚洲免费观看高清完整版在线观看熊 | 蜜臀久久99精品久久久久久9 | 久久aⅴ乱码一区二区三区| 最新日韩中文字幕| 久久精品国产第一区二区三区最新章节 | 欧美影院在线播放| 日韩亚洲一区在线播放| 国产午夜精品一区二区三区视频 | 国产综合一区二区| 欧美日韩日本网| 久久成人免费电影| 一区二区三区精品久久久| 欧美不卡视频一区| 欧美一区二区高清在线观看| 亚洲美女啪啪| 在线播放日韩| 国产手机视频一区二区| 欧美视频在线免费看| 欧美插天视频在线播放| 久久精品免费播放| 午夜精品久久久久久99热| 一本久道综合久久精品| 亚洲国产成人在线播放| 久久综合福利| 久久免费高清| 久久精品官网| 欧美一级视频| 亚洲欧美高清| 亚洲欧美成人网| 亚洲主播在线播放| 亚洲视频第一页| 亚洲精品在线免费| 亚洲欧洲日夜超级视频| 在线视频成人| 亚洲国产精品黑人久久久| 在线观看欧美日本| 在线不卡视频| 亚洲国产经典视频| 亚洲国产91| 亚洲乱码精品一二三四区日韩在线 | 欧美日本三区| 欧美深夜福利| 欧美日韩中文精品| 国产精品久久二区| 国产精品久久久久久久久久久久| 欧美日韩中字| 国产精品久久久久久久久久ktv| 国产精品成人观看视频国产奇米| 欧美午夜视频一区二区| 国产精品乱子乱xxxx| 国产精品一区一区| 激情久久影院| 亚洲人成人77777线观看| 9色porny自拍视频一区二区| 在线综合欧美| 午夜精彩视频在线观看不卡| 亚洲欧美一区二区三区极速播放| 午夜久久久久久| 久久亚洲精品中文字幕冲田杏梨| 乱中年女人伦av一区二区| 亚洲国产精品一区在线观看不卡| 亚洲精品久久久久久下一站 | 能在线观看的日韩av| 亚洲国产成人不卡| av成人毛片| 小嫩嫩精品导航| 久久久久久久高潮| 欧美激情一区二区三区在线| 国产精品福利影院| 激情成人av| 一区二区91| 久久久久久久久久码影片| 亚洲第一区色| 亚洲欧美另类在线| 欧美国产视频日韩| 国产欧美日韩在线观看| 亚洲国产一区二区三区a毛片| 亚洲一区二区网站| 你懂的国产精品永久在线| 一区二区三区免费网站| 久久伊人一区二区| 欧美视频免费看| 在线观看一区| 亚洲欧美中文日韩在线| 欧美激情在线有限公司| 亚洲一区二区在线播放| 欧美成人免费在线视频| 亚洲一区二区三区欧美| 免费h精品视频在线播放| 国产日韩精品电影| 亚洲午夜久久久| 欧美肥婆在线| 久久精品视频导航| 国产精品视频第一区| 亚洲精品网址在线观看| 久热精品视频在线观看一区| 亚洲免费一在线| 欧美天天在线| 一区二区三区黄色| 亚洲国产清纯| 久久久久久夜|