I have a method which returns different types of instances (classes):
public [What Here?] GetAnything()
{
Hello hello = new Hello();
Computer computer = new Computer();
Radio radio = new Radio();
return radio; or return computer; or return hello //should be possible?!
}
How can I do this and later work with the variables, e.g. radio.Play(), etc?
Do I need to use generics, and if so, how?
If there is no common base-type or interface, then
public object GetAnything() {...}– but it would usually be preferable to have some kind of abstraction such as a common interface. For example ifHello,ComputerandRadioall implementedIFoo, then it could return anIFoo.