为什么C#不让您使用var声明多个变量? [英] Why doesn't C# let you declare multiple variables using var?
本文介绍了为什么C#不让您使用var声明多个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
提供以下信息:
// not a problem
int i = 2, j = 3;
这让我感到惊讶:
// compiler error: Implicitly-typed local variables cannot have multiple declarators
var i = 2, j = 3;
不会编译.也许对此我有些不了解(这就是为什么我要问这个问题)?
doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)?
但是为什么编译器不会意识到我的意思:
But why wouldn't the compiler realize that I meant:
var i = 2;
var j = 3;
将进行编译.
推荐答案
对于程序员和编译器而言,这只是可能造成混淆的另一点.
It's just another point of possible confusion for the programmer and the compiler.
例如,这很好:
double i = 2, j = 3.4;
但这是什么意思?
var i = 2, j = 3.4;
使用语法糖,这种事情是没人需要的头痛-因此,我怀疑您的情况是否会得到支持.它涉及到太多的编译器,以至于变得过于聪明.
With syntactic sugar this kind of thing is a headache no one needs--so I doubt your case would ever be supported. It involves too much of the compiler trying to be a little bit too clever.
这篇关于为什么C#不让您使用var声明多个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文