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

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>
              亚洲激情在线播放| 亚洲福利视频网站| 欧美一二三视频| 亚洲一区二区毛片| 国产一区二区三区四区老人| 久久天天狠狠| 美日韩精品免费| 亚洲午夜一二三区视频| 午夜精品久久久久久久久久久久| 狠狠色综合网站久久久久久久| 欧美成人免费在线观看| 欧美母乳在线| 久久国产免费看| 欧美阿v一级看视频| 亚洲伊人久久综合| 久久久久久久国产| 亚洲性感激情| 久久久人成影片一区二区三区| 亚洲美女黄网| 性高湖久久久久久久久| 亚洲日本理论电影| 亚洲欧美制服另类日韩| 亚洲日本aⅴ片在线观看香蕉| 亚洲视频在线看| 亚洲国产一二三| 亚洲影视在线播放| 亚洲日本精品国产第一区| 午夜精品久久99蜜桃的功能介绍| 亚洲国产一区在线| 亚洲欧美日韩专区| 一区二区三区欧美亚洲| 久久精品国产亚洲一区二区三区 | 欧美日本二区| 久久精品欧洲| 欧美日韩一区二区三区在线| 美日韩免费视频| 国产精品三上| 亚洲精品你懂的| 国产在线高清精品| 一区二区国产在线观看| 亚洲人成网站影音先锋播放| 亚洲欧美精品在线| 亚洲一区二区成人在线观看| 欧美国产日韩视频| 免费黄网站欧美| 国产一二精品视频| 亚洲在线一区二区| 亚洲一本大道在线| 欧美日韩免费区域视频在线观看| 欧美成人性生活| 黄色亚洲大片免费在线观看| 亚洲资源在线观看| 亚洲欧美激情四射在线日 | 一本色道久久88综合日韩精品| 欧美在线日韩在线| 欧美一区二区在线免费观看| 欧美午夜视频在线| 一本大道久久a久久精品综合| 亚洲精品中文字| 欧美激情一区二区| 亚洲国产二区| 99综合精品| 欧美欧美午夜aⅴ在线观看| 亚洲精品乱码视频| 亚洲神马久久| 欧美日韩一区二区在线| 一本高清dvd不卡在线观看| 在线综合亚洲欧美在线视频| 欧美日韩伊人| 亚洲性视频网站| 久久国产精品亚洲77777| 国产日韩在线看片| 久久精品一本| 欧美激情一区二区| 一本一本a久久| 国产精品久久久久久久久搜平片| 亚洲一区三区视频在线观看 | 一本色道久久综合亚洲精品不 | 午夜精品久久久久久久99水蜜桃| 性久久久久久久久| 狠狠色狠狠色综合人人| 老司机67194精品线观看| 亚洲激情视频在线观看| 夜夜爽夜夜爽精品视频| 国产精品播放| 久久精品五月| 亚洲精品国产日韩| 久久都是精品| 亚洲国产精彩中文乱码av在线播放| 欧美成人一区在线| 亚洲影音先锋| 欧美激情性爽国产精品17p| 中日韩视频在线观看| 国产亚洲精品一区二区| 美女国内精品自产拍在线播放| 日韩视频免费| 久久人人97超碰精品888| 亚洲伦理在线| 国产一区二区三区久久| 欧美激情亚洲自拍| 久久国产精品高清| 99精品99| 欧美成人精品高清在线播放| 亚洲专区在线| 亚洲高清av在线| 国产精品丝袜白浆摸在线| 免费欧美视频| 欧美伊人久久久久久午夜久久久久 | 中文精品一区二区三区| 国产一区二区三区的电影| 欧美日韩理论| 美乳少妇欧美精品| 香蕉国产精品偷在线观看不卡| 亚洲电影在线看| 久久精品国语| 午夜精品福利在线观看| 99国内精品久久| 亚洲大胆视频| 国产午夜精品全部视频播放| 欧美三区免费完整视频在线观看| 久久精品在线视频| 亚洲欧美日韩在线不卡| 亚洲天堂av高清| 最新亚洲激情| 亚洲国产二区| 欧美激情视频免费观看| 裸体歌舞表演一区二区 | 亚洲电影免费在线观看| 国产欧美日韩亚洲一区二区三区| 欧美日韩成人网| 欧美国产精品va在线观看| 久热国产精品| 猫咪成人在线观看| 免费成人av在线看| 久久中文精品| 男人的天堂成人在线| 久久久综合精品| 另类天堂视频在线观看| 久久亚洲高清| 久久亚洲精选| 男女av一区三区二区色多| 免费成人av在线看| 欧美激情视频一区二区三区免费| 免费短视频成人日韩| 免费在线国产精品| 欧美久久一区| 欧美性视频网站| 国产欧美激情| 狠狠色伊人亚洲综合网站色| 一区二区亚洲精品| 亚洲欧洲在线一区| 亚洲精品影院在线观看| 亚洲最黄网站| 午夜精品福利一区二区三区av| 欧美一区二区免费| 久久男人资源视频| 亚洲电影成人| 一区二区三区四区国产| 亚洲影院污污.| 久久爱www久久做| 蜜臀av性久久久久蜜臀aⅴ| 欧美激情在线有限公司| 国产精品白丝黑袜喷水久久久 | 国产精品久久999| 国产欧美一区二区三区沐欲 | 亚洲精品久久在线| 亚洲一区二区黄| 久久精品国产77777蜜臀 | 中国日韩欧美久久久久久久久| 午夜精品婷婷| 美女主播视频一区| 亚洲美女性视频| 欧美在线免费播放| 欧美二区视频| 国产欧美日韩| 亚洲美女一区| 久久riav二区三区| 亚洲国产裸拍裸体视频在线观看乱了中文 | 亚洲第一精品福利| 亚洲少妇最新在线视频| 久久香蕉国产线看观看av| 亚洲蜜桃精久久久久久久| 午夜亚洲精品| 欧美日韩视频在线一区二区观看视频| 国产色产综合色产在线视频 | 国产日本欧美一区二区三区在线 | 国产欧美日韩激情| 亚洲精选视频免费看| 久久精品国产亚洲精品| 亚洲精品一区二区三区在线观看| 久久国产精品一区二区| 欧美日韩一区免费| 亚洲欧洲精品一区二区| 久久精品首页| 亚洲在线黄色| 欧美日韩在线三级| 亚洲精品美女久久7777777| 久久久亚洲国产天美传媒修理工 | 久久久91精品国产一区二区三区| 欧美性生交xxxxx久久久|