Practical advantage of generics vs interfaces

前端 未结 8 1510
梦毁少年i
梦毁少年i 2020-11-29 02:35

What would be a practical advantage of using generics vs interfaces in this case:

void MyMethod(IFoo f) 
{
}

void MyMethod(T f) : where T : IFoo
{
         


        
8条回答
  •  春和景丽
    2020-11-29 03:24

    2 years later I found a very simple and useful case. Consider this common pattern:

    class MyClass : IDisposable {
    
         public void Dispose() {
             if (m_field1 != null) {
                 m_field1.Dispose();
                 m_field1 = null;
             }
             if (m_field2 != null) {
                 m_field2.Dispose();
                 m_field2 = null;
             }
             // etc
         }
    }
    

    I've always wanted to write a helper method to avoid having to write all this boilerplate for every field:

    class MyClass : IDisposable {
    
        static void IfNotNullDispose(ref IDisposable disposable) {
            if (disposable != null) {
                disposable.Dispose();
                disposable = null;
            }
        }
    
        public void Dispose() {
             IfNotNullDispose(ref m_field1);
             IfNotNullDispose(ref m_field2);
             // etc
        }
    }
    

    Unfortunately this is illegal in C# because you cannot use an interface for ref parameters, you must use the concrete type you'll pass in and nothing else. So you'd have to write a different method for every single type of field you want to dispose. Oh wait that's exactly what generics do for you:

    static void IfNotNullDispose(ref T disposable) where T: class, IDisposable {
        if (disposable != null) {
            disposable.Dispose();
            disposable = null;
        }
    }
    

    Now everything works as intended!

提交回复
热议问题