C# Return Different Types?

后端 未结 15 2051
无人及你
无人及你 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:51

    Marc's answer should be the correct one, but in .NET 4 you couldn't also go with dynamic type.

    This should be used only if you have no control over the classes you return and there are no common ancestors ( usually with interop ) and only if not using dynamic is a lot more painful then using(casting every object in every step :) ).

    Few blog post trying to explain when to use dynamic: http://blogs.msdn.com/b/csharpfaq/archive/tags/dynamic/

    public dynamic GetSomething()
    {
         Hello hello = new Hello();
         Computer computer = new Computer();
         Radio radio = new Radio(); 
         return // anyobject
    
    }
    

提交回复
热议问题