在C#中匿名类型的字段

在C#中匿名类型的字段

本文介绍了我如何申报"关键"在C#中匿名类型的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET我已经习惯了这样搞创建匿名类型时(的概念):

In VB.NET I'm used to doing things like this when creating anonymous types (VB.NET anonymous types include the notion of Key Fields):

Dim prod1 = New With {
    Key .Name = "paperclips",
    Key .Price = 1.29,
    .OnHand = 423
}

不过,我一直没能找到在C#这样做的任何方式,因为它似乎不支持关键字。

However, I haven't been able to find any way of doing this in C#, since it appears the Key keyword is not supported.

有没有办法在C#中,表示在寻找平等?

Is there any way to indicate in C# that I only want to compare some of the fields in anonymous type when looking for equality?

推荐答案

有没有像在C#。 ,匿名类型的所有属性是只读,并参与平等的比较。

There is nothing like that in C#. In C#, all properties of anonymous types are read-only and participate in equality comparisons.

如果你想要做这样的事情在C#中,您将需要创建自己的(命名)类型。

If you want to do something like this in C#, you will need to create your own (named) type.

这篇关于我如何申报"关键"在C#中匿名类型的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:03