Javascript “var obj = new Object” Equivalent in C#

前端 未结 4 1879
别跟我提以往
别跟我提以往 2021-02-07 10:49

Is there an easy way to create and Object and set properties in C# like you can in Javascript.

Example Javascript:

var obj = new Object;

obj.value = 123         


        
4条回答
  •  难免孤独
    2021-02-07 11:15

    try c# anonymous classes

    var obj = new { 
        value = 123475, 
        description = "this is my new object", 
        mode = 1 };
    

    there are lots of differences though...

    @Valera Kolupaev & @GlennFerrieLive mentioned another approach with dynamic keyword

提交回复
热议问题