是否有对C的内嵌匿名结构定义一个等效C#语法? [英] Is there an equivalent C# syntax for C's inline anonymous struct definition?

查看:131
本文介绍了是否有对C的内嵌匿名结构定义一个等效C#语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候Overflowers,

Greetings Overflowers,

我知道用C这样的结构类型是专用于这个变量,我们可以定义一个变量声明一个结构内嵌。这是不是单独定义的类型,然后声明变量是结构类型。在C#这是可能的吗?

I know in C we can define a struct inline with the variable declaration so that the struct type is specific to this variable. This is instead of defining the type alone then declaring the variable to be of that struct type. Is this possible in C#?

感谢!

推荐答案

这是不是在C#可能的,但是你可以定义匿名类型这样的实例:

This is not possible in C#, however you can define an instance of an anonymous type like this:

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.

这篇关于是否有对C的内嵌匿名结构定义一个等效C#语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆