Does anyone else feel that it might be useful if the runtime allowed references to members of a generic interface that were not specific to the generic type? I know the usual "workaround" is to create a non-generic interface as the base for the generic interface, but is there a valid reason against that base interface basically being automatic?
For example, given the following interface:
public interface IProcessor<T>
{
string Name { get; }
void Process(T item);
}
I think it would be convenient to automatically allow something like this:
public void LogProcessor(IProcessor<> item)
{
Trace.WriteLine(item.Name);
}
I'm curious to hear arguments against this (other than "stop being so lazy and just write the base interface").
An interface is abstract so that it can't provide any code. An abstract class can give complete, default code which should be overridden. Use of Access modifiers. You cannot use access modifiers for the method, properties, etc. You can use an abstract class which contains access modifiers.
A generic interface is primarily a normal interface like any other. It can be used to declare a variable but assigned the appropriate class. It can be returned from a method. It can be passed as argument. You pass a generic interface primarily the same way you would an interface.
Abstraction in interfaces The user who want to use the methods of the interface, he only knows the classes that implement this interface and their methods, information about the implementation is completely hidden from the user, thus achieving 100% abstraction.
The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).
You can just use a generic method:
public void LogProcessor<T>(IProcessor<T> item)
{
Trace.WriteLine(item.Name);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With