为什么c#需要“var”标识符?

发布于 2024-11-02 15:02:43 字数 465 浏览 1 评论 0原文

可能的重复:
var 关键字有什么意义?

为什么 c# 需要 " var”类型推断类型变量的标识符?

我的意思是,不写它会有什么问题:

a = 1;
//vs
var a = 1;

ReadingProgramming in Scala:

“类型变量”语法不能简单地忽略类型 - 将不再有标记来开始定义。

但是将其保留在 a:Intint a 中有何不同?

Possible Duplicate:
What's the point of the var keyword?

Why does c# need the "var" identifier for type inferred type variables?

I mean what is the problem with just leaving it off:

a = 1;
//vs
var a = 1;

Reading Programming in Scala:

"Type variable" syntax you cannot simply leave off the type - there would be no marker to start the definition anymore.

But what is the different between leaving it off in the a:Int or int a?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

少跟Wǒ拽 2024-11-09 15:02:43

至少想到的一个原因是,它迫使程序员声明他们引入新变量的意图,而不是分配给现有变量。这使得编译器能够检测到许多常见的编码错误。

At least one reason that comes to mind is that it forces the programmer to declare their intent to introduce a new variable, as opposed to assigning to an existing variable. This enables the compiler to detect numerous common coding errors.

百变从容 2024-11-09 15:02:43

考虑一下:

class Foo
{
    int a;

    void Bar()
    {
        var a = 1;
    }
}

如果没有 var 关键字,赋值将是给类成员 a

var 明确引入了一个新的局部变量。

Consider this:

class Foo
{
    int a;

    void Bar()
    {
        var a = 1;
    }
}

Without the var keyword, the assignment would be to the class member a.

var introduces unambiguously a new local variable.

伴梦长久 2024-11-09 15:02:43

在 C# 中使用之前必须先定义变量。当然,编译器可以检测到它何时到达

a = 1;

That a had not not be returned ,因此定义该变量并将其赋值为 1 。但是,这可能会导致您遇到的其他问题:

MyOwnClass myVeryLongVariableNameThatIsUsedAllOverThePlace = new MyOwnClass();
myveryLongVariableNameThatIsUsedAllOverThePlace = input.GetNewClass();

现在您有 2 个变量,您认为你有一个。

You have to define a variable before you use in C#. Sure, the compiler could detect when it reaches

a = 1;

That a had not yet been defined and so define the variable and assign it the value of 1. However, it could lead to other issues where you have:

MyOwnClass myVeryLongVariableNameThatIsUsedAllOverThePlace = new MyOwnClass();
myveryLongVariableNameThatIsUsedAllOverThePlace = input.GetNewClass();

Now you have 2 variables, where you thought you had one.

蓦然回首 2024-11-09 15:02:43

来自 MSDN:

重要的是要了解
var 关键字并不意味着“变体”
并且并不表明
变量是松散类型的,或者
晚绑定。这只是意味着
编译器确定并分配
最合适的类型。

这个想法是通过防止意外的隐式类型局部变量。

From MSDN:

It is important to understand that the
var keyword does not mean "variant"
and does not indicate that the
variable is loosely typed, or
late-bound. It just means that the
compiler determines and assigns the
most appropriate type.

The idea is to keep the robustness of C# by preventing accidental Implicitly Typed Local Variable.

飘过的浮云 2024-11-09 15:02:43

从技术上来说,确实没有必要。其他语言允许您将声明和赋值结合起来,无需特殊关键字。

如果 a = 1 对于赋值和声明来说都是有效的语法,那么代码可能会变得非常混乱,我相信这就是为什么 C# 至少需要 var

Technically, there's really no need. Other languages allow you to combine declaration and assignment with no special keywords.

If a = 1 was valid syntax for both assignment and declaration, though, code could become very confusing which is, I believe, why C# requires at least var.

要走干脆点 2024-11-09 15:02:43

关键字将告诉编译器不要尝试解析方法范围之外标识符

如果没有它,C# 还能工作吗?是的,但这强制执行了强类型语言所需的规则

keyword will tell the compiler not to try resolve the identifier outside the scope of the method.

Can C# work without it? Yes, but this enforces a discipline which is required for a strongly typed language.

小巷里的女流氓 2024-11-09 15:02:43

不同的编程语言被设计来适应不同的目的。 C# 专为编写大型高可靠性软件而设计。其方法的一部分是使用显式声明的静态类型变量。添加匿名对象需要推断变量类型的能力,而匿名对象是 LINQ 查询表达式所必需的。这些东西的存在并没有改变这样一个事实:变量必须显式声明,并且它们的类型在编译时建立(当然,模动态)。

显式变量声明消除了用不需要变量声明的语言编写的程序中有时会出现的一大类错误。

Different programming languages are designed to suit different purposes. C# is designed for writing large-scale high-reliability software. Part of its approach to this is using explicitly-declared staticly-typed variables. The ability to infer variable types was required by the addition of anonymous objects, which were required for LINQ query expressions. The existance of these things does not alter the fact that variables must be explicity-declared and their types established at compile-time (modulo dynamic, of course).

Explicit variable declaration removes a large class of bugs that sometimes occur in programs written in languages that do not require variable declarations.

明明#如月 2024-11-09 15:02:43

以我的愚见,我认为可以安全地假设他们没有(或不能)如此显着地修改 C# 语法,以至于允许在变量声明中省略(类型或“var”)标记。为语言添加了类型推断。 (这也会对函数声明产生影响。)

我认为这样的更改会对整个语法产生连锁反应,并破坏向后兼容性。

也许 F# 是在如此基础的层面上改变语法的副产品;)

In my humble opinion, I think it is safe to assume that they didn't (or couldn't) modify the C# grammar so significantly as to allow the omission of a (type or "var") token in the variable declaration when they added type inference to the language. (This would have consequences for function declarations also.)

I think such a change would have a ripple effect across the entire grammar and wreck backwards compatibility.

Maybe F# is a by-product of the grammar being changed at such a fundamental level ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文