使用 var 真的很慢吗?如果是这样,为什么? [英] Is using var actually slow? If so, why?

查看:41
本文介绍了使用 var 真的很慢吗?如果是这样,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C# 和 .NET,我经常在我的代码中使用关键字 var.我从 Eric Lippert 那里得到了这个想法,我喜欢它如何提高我的代码的可维护性.

I am learning C# and .NET, and I frequently use the keyword var in my code. I got the idea from Eric Lippert and I like how it increases my code's maintainability.

不过,我想知道...博客中已经写了很多关于慢堆定位引用的文章,但我自己并没有观察到这一点.这真的很慢吗?我指的是由于类型推断导致的编译时间缓慢.

I am wondering, though... much has been written in blogs about slow heap-located refs, yet I am not observing this myself. Is this actually slow? I am referring to slow compile times due to type inferencing.

推荐答案

你说:

我指的是由于类型推理"而导致编译时间变慢

I am referring to slow time for compile due to type 'inferencing'

这不会减慢编译器的速度.编译器已经必须知道表达式的结果类型,以便检查赋值的兼容性(直接或间接).在某些方面,使用这种已知类型消除一些事情(例如,必须检查继承、接口和转换运算符的可能性).

This does not slow down the compiler. The compiler already has to know the result type of the expression, in order to check compatibility (direct or indirect) of the assignment. In some ways using this already-known type removes a few things (the potential to have to check for inheritance, interfaces and conversion operators, for example).

它也不会减慢运行时间;它们是完全静态编译的,就像常规的 c# 变量(它们是).

It also doesn't slow down the runtime; they are fully static compiled like regular c# variables (which they are).

简而言之...它没有.

In short... it doesn't.

这篇关于使用 var 真的很慢吗?如果是这样,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆