简体   繁体   English

C的内联匿名结构定义是否有等效的C#语法?

[英]Is there an equivalent C# syntax for C's inline anonymous struct definition?

Greetings Overflowers, 问候Overflowers,

I know in C we can define a struct inline with the variable declaration so that the struct type is specific to this variable. 我知道在C中我们可以使用变量声明定义一个内联结构,以便结构类型特定于此变量。 This is instead of defining the type alone then declaring the variable to be of that struct type. 这不是单独定义类型,而是将变量声明为该结构类型。 Is this possible in C#? 这可能在C#中吗?

Thank ! 谢谢 !

This is not possible in C#, however you can define an instance of an anonymous type like this: 这在C#中是不可能的,但是你可以像这样定义一个匿名类型的实例:

var x = new { SomeField = 1, SomeOtherField = "Two" }; 

This would effectively be the same, giving you an instance of a type that is specific to that variable and cannot used outside the variable's scope. 这实际上是相同的,为您提供特定于该变量的类型的实例,并且不能在变量的范围之外使用。

简单回答:不,这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM