dynamic return type of a function

后端 未结 5 1089
春和景丽
春和景丽 2021-02-07 04:46

How can I create a function that will have a dynamic return type based on the parameter type?

Like

protected DynamicType Test(DynamicType type)
{

retur         


        
5条回答
  •  故里飘歌
    2021-02-07 05:14

    You'd have to use generics for this. For example,

    protected T Test(T parameter)
    {
    
    }
    

    In this example, the '' tells the compiler that it represents the name of a type, but you don't know what that is in the context of creating this function. So you'd end up calling it like...

    int foo;
    
    int bar = Test(foo);
    

提交回复
热议问题