Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
问候溢出者,
我知道在 C 中我们可以定义一个与变量声明内联的结构,以便结构类型特定于该变量。这不是单独定义类型,然后将变量声明为该结构类型。这在 C# 中可能吗?
谢谢 !
这在 C# 中是不可能的,但是您可以像这样定义匿名类型的实例:
var x = new { SomeField = 1, SomeOtherField = "Two" };
这实际上是相同的,为您提供特定于该变量且不能在变量范围之外使用的类型的实例。
简单的回答:不,这是不可能的。