Inheritance of the main interface (design problem)

I have two interfaces; ISetandISet<T>

Both implement Add, Remove, and Clear, but with different types of arguments; object and T.

Now I have two options. Either let it ISet<T>inherit from ISet, or let isolate them from each other.

One of the benefits of using ISet<T>inheritance from ISetis, of course, that I can always use ISet<T>where I need it ISet. But at the same time I also need to add a “new” modifier to members with a T parameter. I don’t really like the “new” modifier, but maybe this is the best option in this case?

It seems strange to have ISet<T>, which is also not ISet. This is a logical assumption. I can’t say why I don’t like the “new” modifier. This is like "goto" - a keyword. I just try not to use it.

Do you think I should go here? Inherit or not?


In .NET, we have ICollection<T>and ICollectionthat are not inherited. Also not performed IList<T>and IList. But IEnumerable<T>inherits from IEnumerable.


I know about an ISet interface that already exists (this was just an example)

+3
source share
7 answers

, - , - ISet<T>, , T. , ( ), , Count, .

, - ( , , , , , )

interface ICountable { int Count {get;} }
interface IClearable { int Count {get;} }
interface IAppendable<in T> { void Add(T item); }
interface ICountableEnumerable<out T> : IConvertableToEnumerable, ICountable 
  {IEnumerable<T&gt CopyAsEnumerable();}
interface IFetchable<out T> { T FetchAndRemove(ref bool wasNonEmpty); }
interface ISet<T> ICountable, IClearable, IAppendable<T>, IFetchable<T>,
          IConvertableToEnumerable<T>; 

, .

+1

. , ISet<T> , ISet . . BaseSet<T>, ISet<T>, ISet

public abstract class BaseSet<T> : ISet<T>, ISet {
  public abstract void Add<T>(T item);

  void ISet.Add(object item) {
     this.Add((T)item);
  } 
}

BaseSet<T> ISet<T> , ISet<T> ISet, BaseSet<T> , . new.

+4

, .NET-, ?

interface ISet : ISet<object>
{ }

interface ISet<T>
{
    void Add<T>(T item);
    void Clear();
    void Remove<T>(T item);
}

, , .NET, ?

+1

ISet Object T, ?

+1

, , , -. ? , , . ...

0

ISet?.NET , . , .NET .

, .NET IList, IList<T>. , , - , , .NET . , , IList , IList<T> .

0

, IEnumerable<T> IEnumerable. foreach, . IEnumerable ( ). foreach:

E enumerator = (collection).GetEnumerator(); // non-generic interface
try {
   while (enumerator.MoveNext()) { // enumerator also non-generic
      ElementType element = (ElementType)enumerator.Current;
      statement;
   }
}
finally {
   IDisposable disposable = enumerator as System.IDisposable;
   if (disposable != null) disposable.Dispose();
}

So, we have common interfaces that inherit from non-generic to allow the use of shared collections in the foreach statement. Another option is to change the compiler :)

There are no core .net functions that are interface dependent IList. So, we have two independent interfaces - common and not common.

In your case, I see no reason to create a non-common interface ISet. If you want to have a method that accepts a generic one ISet<T>with different type parameters, then just create a generic method that accepts a generic one ISet<T>:

public void Foo<T>(ISet<T> set)
{
    set.Bar();
}
0
source

All Articles