在一个语句中的多个内嵌分配在C# [英] Multiple inline assignments in one statement in c#
本文介绍了在一个语句中的多个内嵌分配在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
显然,以下是在C#完全罚款;
int类型的;
INT B = A = 2;
是否有可能做在C#中的多个变量赋值在一个语句?
即像;
int类型的=(INT B = 2);
解决方案
如果我们看一下:
int类型的;
INT B = A = 2;
这实质上是 A = 2;
然后 b = A;
(但没有额外的EVAL)。因此,我们可以通过颠倒顺序得到类似的:
int类型的= 2,B = A;
不过:我会说拿这个有点欲言又止:也请优先考虑可读性
Obviously the following is totally fine in c#;
int a;
int b = a = 2;
Is it possible to do multiple variable assignments in c# in a single statement?
i.e. something like;
int a = (int b = 2);
解决方案
If we look at:
int a;
int b = a = 2;
That is essentially a=2;
then b=a;
(but without an extra eval). So we can get similar by reversing the order:
int a = 2, b = a;
However: I would say take this a bit hesitantly: please also prioritise readability.
这篇关于在一个语句中的多个内嵌分配在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文