C# Return Different Types?

后端 未结 15 2053
无人及你
无人及你 2020-12-08 09:11

I got something like this:

public [What Here?] GetAnything()
{
     Hello hello = new Hello();
     Computer computer = new Computer();
     Radio radio = ne         


        
15条回答
  •  眼角桃花
    2020-12-08 09:47

    Here is how you might do it with generics:

    public T GetAnything()
    {
       T t = //Code to create instance
    
       return t;
    }
    

    But you would have to know what type you wanted returned at design time. And that would mean that you could just call a different method for each creation...

提交回复
热议问题