0

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);;
    }
}
tbhaxor
  • 1,659
  • 2
  • 13
  • 43
  • 3
    It's an [anonymous type](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types), you can find more information at [Anonymous Types C#](https://stackoverflow.com/questions/41250314/anonymous-types-c-sharp) and [How to dynamic new Anonymous Class?](https://stackoverflow.com/questions/3740021/how-to-dynamic-new-anonymous-class) – Pavel Anikhouski Mar 29 '20 at 08:00

1 Answers1

4

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.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36