在C#中的对象和VAR的区别

编程入门 行业动态 更新时间:2024-10-18 22:27:23
本文介绍了在C#中的对象和VAR的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

之间有什么区别对象和 VAR ?

推荐答案
  • 的 VAR - 不指定类型明确。让编译器弄明白该类型是什么。
    • 类型是固定在设计时,不能参照其他类型的对象。
    • 作为保利在评论中指出的那样,你会得到智能感知。
    • 必须初始化。 VAR我; 将无法编译
    • 不能用作方法的返回类型
    • 必须是一个局部变量。不是字段或属性。
    • 伟大工程具有的 匿名类型 。你得到智能感知。

    • var - Not specifying the type explicitly. Letting compiler figure out what that type is.
      • Type is fixed at design time and cannot refer to object of other type.
      • As Pauli noted in a comment, you get intelliSense.
      • Must be initialized. var i; won't compile.
      • Cannot be used as return type of a method.
      • Must be a local variable. Not a field or property.
      • Works great with Anonymous Types. You get intelliSense.
        • 可用于在运行时指任何类型的
        • 在这里,你没有得到智能感知。
        • Can be used to refer any type at runtime.
        • Here you don't get intelliSense.
        var i = 0; // i is of type `System.Int32`. Same as "int i = 0;" i = "Some String"; // Compile time error. object o = 0; o = "Some String"; // Works

更多推荐

在C#中的对象和VAR的区别

本文发布于:2023-11-11 23:09:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   对象   VAR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!