Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract references to generic interfaces

Tags:

c#

generics

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").

like image 663
WuffaloWill Avatar asked Mar 18 '11 19:03

WuffaloWill


People also ask

What is an abstract 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.

What is a generic interface?

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.

How does interface related to abstraction?

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.

Can we use abstract in interface?

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).


1 Answers

You can just use a generic method:

public void LogProcessor<T>(IProcessor<T> item)
{
    Trace.WriteLine(item.Name);
}
like image 69
Reed Copsey Avatar answered Sep 24 '22 00:09

Reed Copsey