Click here to Skip to main content
14,734,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the best way of creating an interface to return a list of 'things' or objects
for example:

public interface MyInterface
   List<object> ComputeSomething(string input)

Of course the above is not correct right?

Updated 13-Jan-16 15:43pm
PIEBALDconsult 13-Jan-16 20:43pm
Try it.
Dave Kreskowiak 13-Jan-16 21:18pm
Did you try implementing it? Experimentation is the best teacher.

You might find that returning a List<Object> is really not a good idea.
DamithSL 13-Jan-16 21:54pm
use another interface IObject and create
List<Iobject> ComputeSomething(string input)

How about one of these two options?

public interface MyInterface
    List<T> ComputeSomething<T>(string input);

public interface MyInterface<T>
    List<T> ComputeSomething(string input);
Missing is the character ';' at the end of method declaration. Without it, your code won't compile.

As to the purpose of that… I doubt there is any. And without the purpose, no one can say if something is right or wrong. It can be wrong for one purpose and right for another one.

In this case, I doubt it can make any sense, because System.Object is the base type of all types, so your list is essentially untyped. At least it will lead to the very basic violation of OOP. Perhaps you have to learn dynamic dispatch and polymprphism, that is, the basics of OOP. Even more suspicious is generation of some list out of some string. It pretty strongly suggests that you tend to work with strings representing data instead of using data itself.

If you still have more questions on the topic, you are very welcome to ask them, but we can discuss something else only if you share your ultimate goals.


Also note that your interface is not generic. An example of generic interface would be, for example,
interface MyInterface<ARGUMENT, RETURN>
    RETURN SomeFunction(ARGUMENT arg);
    void SomeMethod();
    void SomeOtherMethod(ARGUMENT arg);
    // ...


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900