I got something like this:
public [What Here?] GetAnything()
{
Hello hello = new Hello();
Computer computer = new Computer();
Radio radio = ne
It's a sample using Generic Types.
public T GetAnything() where T : class, new()
=> new T();
And you'll use this method calling this way:
var hello = GetAnything();
In this case, you can use an interface to specify a type to pass as parameter.
public T GetAnything() where T : ISomeInterface, new()
=> new T();
You must have a parameterless construtor in each class to use new() constraint.
Follow the full sample:
internal sealed class Program
{
private static void Main(string[] args)
{
GetAnything().Play();
GetAnything().Play();
GetAnything().Play();
}
private static T GetAnything() where T : ISomeInterface, new()
=> new T();
}
internal interface ISomeInterface
{
void Play();
}
internal sealed class Hello : ISomeInterface
{
// parameterless constructor.
public Hello() { }
public void Play() => Console.WriteLine("Saying hello!");
}
internal sealed class Radio : ISomeInterface
{
// parameterless constructor.
public Radio() { }
public void Play() => Console.WriteLine("Playing radio!");
}
internal sealed class Computer : ISomeInterface
{
// parameterless constructor.
public Computer() { }
public void Play() => Console.WriteLine("Playing from computer!");
}