简体   繁体   中英

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

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

Although this seems to be similar to the JSON type. But when I tried to use the GetType method, I got <>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.

You can read about it FROM 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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