相关疑难解决方法(0)

C#返回不同的类型?

我有这样的事情:

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?!      
}
Run Code Online (Sandbox Code Playgroud)

我有一个方法,这个方法有时会返回不同类型的值(类).

我怎么能这样做,以后再使用变量,是radio.Play(); 到目前为止?

我需要使用泛型吗?怎么样?

c# types

46
推荐指数
6
解决办法
11万
查看次数

标签 统计

c# ×1

types ×1