What is the Difference Between `new object()` and `new {}` in C#?

前端 未结 3 1017
温柔的废话
温柔的废话 2020-12-24 04:43

First of all i searched on this and i found the following links on Stack Overflow:

  • Is there any difference between `new object()` and `new {}` in c#?
3条回答
  •  执笔经年
    2020-12-24 05:47

    new{ } creates an instance of an anonymous type with no members. This is different from creating an instance of object. But like almost all types, anonymous types can be assigned to object.

     object data = new { };
     Console.WriteLine(data.GetType().Name)
    

    Clearly shows an auto-generated name, not Object.

提交回复
热议问题