How can a dynamic be used as a generic?

前端 未结 6 460
南旧
南旧 2021-01-02 06:08

How can I use a dynamic as a generic?

This

var x = something not strongly typed;
callFunction();

and this

         


        
6条回答
  •  既然无缘
    2021-01-02 07:06

    You get that error because x is not a type. You need to specify a type as a type parameter.

    In fact, you can use dynamic as a type parameter if you use it correctly:

    var dict = new Dictionary();
    
    dict.Add("Item1", 123);
    dict.Add("Item2", "Blah");
    

    This compiles and runs just fine.

提交回复
热议问题