new { }(不带 [])是什么意思?

What is the meaning of new { } (without [])?

我正在为 class 在 NUnit 上做一个类似的研究项目,我们的老师写了这个测试:

[Test]
    public void ObjectLiteralIsAnObject ()
    {
        Assert.AreEqual (true, new { } is Object);
    }

我知道 new[]{} 的含义,它根据您在 {} 之间插入的值创建一个最佳可分配类型的数组。但是 new { } 做了什么?是一样的,还是有细微的差别?

在此先感谢您,如果这是一个愚蠢的问题,我们深表歉意!

new { }

用于创建匿名对象,即无名对象。

常用于LINQ查询,例如:

var query =
    from c in db.Customers
    select new
    {
        c.CustomerId,
        c.CustomerName
    };

这将创建具有两个属性的匿名类型 IQueryableCustomerIdCustomerName