简体   繁体   English

C# 中类似 JSON 的对象的实际类型是什么 (new {};)

[英]What is the actual type of JSON like object in C# (new {};)

I was learning C# and found that there is some type of value defined by我正在学习 C# 并发现有某种类型的值由

var json = new {name="App", age=20};

Although this seems to be similar to the JSON type.虽然这似乎类似于 JSON 类型。 But when I tried to use the GetType method, I got <>f__AnonymousType0`2[System.String,System.Int32]但是当我尝试使用 GetType 方法时,我得到了<>f__AnonymousType0`2[System.String,System.Int32]

Can anyone help me in this please?任何人都可以帮助我吗?

In case you want the full code如果你想要完整的代码

using System;

public class Program
{
    public static void Main()
    {
        var c = new { name="App", age=22 };

        Console.WriteLine(c.GetType());
        Console.WriteLine(c);;
    }
}

It's called Anonymous Type and it has no relation to JSON.它称为Anonymous Type ,与 JSON 无关。

You can read about it FROM MSDN你可以从MSDN阅读它

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.匿名类型提供了一种方便的方法,可以将一组只读属性封装到单个对象中,而无需先显式定义类型。 The type name is generated by the compiler and is not available at the source code level.类型名称由编译器生成,在源代码级别不可用。 The type of each property is inferred by the compiler.每个属性的类型由编译器推断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM