C#编译器如何解释var关键字指定的对象?

发布于 2024-08-21 20:29:47 字数 44 浏览 2 评论 0原文

C#编译器如何解释var关键字指定的对象?什么时候我们必须使用这个关键字?

How C# compiler interpret objects that specified by var keyword? When do we must use this keyword?

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

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

发布评论

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

评论(4

恏ㄋ傷疤忘ㄋ疼 2024-08-28 20:29:47

有时您需要使用 var,例如,在 LINQ 查询中使用匿名类型时:

var results = context.People.Select(p => new {p.PersonID, p.Name});

看到其中的 new{} 行了吗?这将返回一个在编译时生成的类。由于此类没有名称,因此在代码中引用它的唯一方法是使用“var”。这减少了您必须为 LINQ 结果集创建大量专用类的情况。

在幕后,无论您使用 var 作为另一种类型的简写,还是使用匿名类型,您仍然在进行静态的编译时类型检查。未使用动态类型。编译器在编译时会从字面上找出类型应该是什么,因此像这样的程序

var i = 12;
i = i + "foo!";

将无法编译。

There are times when you need to use var, for example, when using an anonymous type, such as in a LINQ query:

var results = context.People.Select(p => new {p.PersonID, p.Name});

See the new{} line in there? That's returning a class which is being generated at compile-time. Since this class has no name, the only way you can refer to it in your code is by using "var". This cuts down on you having to create tons and tons of special-purpose classes just for LINQ resultsets.

Under the hood, whether you use var just as a shorthand for another type, or if using an anonymous type, you are still doing static, compile-time type checking. Dynamic types are not being used. The compiler literally figures out what the type should be when you compile, so a program like:

var i = 12;
i = i + "foo!";

won't compile.

荒岛晴空 2024-08-28 20:29:47

var(C# 参考)

从 Visual C# 3.0 开始,以下变量在方法作用域声明的可以具有隐式类型 var。隐式类型局部变量是强类型的,就像您自己声明类型一样,但编译器确定类型。

var (C# Reference)

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

始终不够 2024-08-28 20:29:47

在 Var 声明中,

编译器从
指定值

为什么

var a = 100;     // compiler assumes a is a integer
var b = "test";  // compiler assumes b is a string

我们需要它们(为什么我们不能直接使用对象)

因为对象不强制类型安全,

  object objtest = 1;
  objtest = "test";

所以这工作得很好。

但是 var 强制执行类型安全,

  var a = 100;    
  a= "test";  

这不会编译,并且会给出编译时错误。

我们可以在任何地方使用它们

  • Linq 查询返回匿名类型
  • 以及任何您想要定义类型安全变量(简单)的地方。而不是写作
    像这样的东西。

   RootClass rt = new RootClass ();
   List<RootClass > rt = new List<RootClass >();

你可以这样写

var aaaaaa = new RootClass ();
var ls = new List<RootClass>();

In Var declaration,

Compiler infer the type from the
assigned value

on

var a = 100;     // compiler assumes a is a integer
var b = "test";  // compiler assumes b is a string

Why do we need them (why cant we use objects directly)

Because object doesnt enforce type safety

  object objtest = 1;
  objtest = "test";

this works fine.

But var enforces type safety

  var a = 100;    
  a= "test";  

this doesnt compile, and will give the compile time error.

Where we can use them

  • Linq queries returning Anonymous types
  • And anywhere you want to define type safe variables (simplicity) . instead of writing
    something like this.

.

   RootClass rt = new RootClass ();
   List<RootClass > rt = new List<RootClass >();

you can write like this

var aaaaaa = new RootClass ();
var ls = new List<RootClass>();
双马尾 2024-08-28 20:29:47

如果您的意思是变量声明中使用的“var”关键字,那么就不存在“var type”这样的东西。该“var”(在编译时)被解释为从分配给该变量的表达式推断出的类型。

If you mean the "var" keyword used in variable declaration then there is no such thing as "var type". This "var" is interpreted (at compile time) as type inferred from expression that is assigned to the variable.

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