我已经阅读了大量关于c# v4附带的新关键字的文章,但我无法区分“dynamic”和“var”之间的区别。
这篇文章让我思考了一下,但我还是看不出有什么不同。
你是否只能将“var”用作局部变量,但同时用作局部变量和全局变量?
你能显示一些代码没有动态关键字,然后显示相同的代码与动态关键字吗?
我已经阅读了大量关于c# v4附带的新关键字的文章,但我无法区分“dynamic”和“var”之间的区别。
这篇文章让我思考了一下,但我还是看不出有什么不同。
你是否只能将“var”用作局部变量,但同时用作局部变量和全局变量?
你能显示一些代码没有动态关键字,然后显示相同的代码与动态关键字吗?
当前回答
我会解释dynamic和var的区别。
dynamic d1;
d1 = 1;
d1 = "http://mycodelogic.com";
这是可行的。编译器可以重新创建动态变量的类型。 首先,它创建类型为整数,然后编译器将重新创建类型为字符串,但在var的情况下
var v1; // Compiler will throw error because we have to initialized at the time of declaration
var v2 = 1; // Compiler will create v1 as **integer**
v2 = "Suneel Gupta"; // Compiler will throw error because, compiler will not recreate the type of variable
When using the ‘var’ keyword, the type is decided by the compiler at compile time, whereas when using the ‘dynamic’ keyword, the type is decided by the runtime. ‘var’ keyword, a strongly implicitly typed local variable for which the compiler is able to determine the type from the initialization expression - very useful when doing LINQ programming. Compiler doesn't have any information about the dynamic type of variable. so compiler will not show any intelligence .compiler has all information about the stored value of var type so compiler will show intelligence.dynamic type can be passed as function argument and function also can return object typeButvar type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.
其他回答
Var只是普通类型声明的简写,让编译器猜测正确的类型。
Dynamic是一种新的(静态)类型,其中所有检查都在运行时完成,而不是由编译器完成。
这是一个很好的youtube视频,讨论了var VS动态与实际演示。
下面是一个更详细的快照解释。
Var是早期绑定(静态检查),而dynamic是后期绑定(动态计算)。
Var关键字查看右边的数据,然后在编译时决定左边的数据类型。换句话说,var关键字节省了你输入很多东西。看看下面的图像,当我们给出字符串数据和x变量显示字符串数据类型在我的工具提示。
另一方面,动态关键字的目的完全不同。动态对象在运行时计算。例如,在下面的代码中,“长度”属性是否存在将在运行时计算。我故意输入了一个小的“l”,所以这个程序编译良好,但当它实际执行时,当“length”属性被调用时抛出了一个错误(small“l”)。
Var意味着应用静态类型检查(早期绑定)。Dynamic意味着应用动态类型检查(后期绑定)。在代码方面,考虑以下内容:
class Junk
{
public void Hello()
{
Console.WriteLine("Hello");
}
}
class Program
{
static void Main(String[] args)
{
var a = new Junk();
dynamic b = new Junk();
a.Hello();
b.Hello();
}
}
如果您编译它并使用ILSpy检查结果,您将发现编译器添加了一些后期绑定代码,这些代码将处理从b对Hello()的调用,而由于早期绑定应用于a, a能够直接调用Hello()。
例如(ILSpy拆卸)
using System;
namespace ConsoleApplication1
{
internal class Junk
{
public void Hello()
{
Console.WriteLine("Hello");
}
}
}
using Microsoft.CSharp.RuntimeBinder;
using System;
using System.Runtime.CompilerServices;
namespace ConsoleApplication1
{
internal class Program
{
[CompilerGenerated]
private static class <Main>o__SiteContainer0
{
public static CallSite<Action<CallSite, object>> <>p__Site1;
}
private static void Main(string[] args)
{
Junk a = new Junk(); //NOTE: Compiler converted var to Junk
object b = new Junk(); //NOTE: Compiler converted dynamic to object
a.Hello(); //Already Junk so just call the method.
//NOTE: Runtime binding (late binding) implementation added by compiler.
if (Program.<Main>o__SiteContainer0.<>p__Site1 == null)
{
Program.<Main>o__SiteContainer0.<>p__Site1 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Hello", null, typeof(Program), new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}));
}
Program.<Main>o__SiteContainer0.<>p__Site1.Target(Program.<Main>o__SiteContainer0.<>p__Site1, b);
}
}
}
你能做的最好的事情是发现差异是自己写一个小的控制台应用程序,像这样,并用ILSpy自己测试它。
区别就在这里
var is statically typed (compile time), dynamic is dynamically typed (run time) A variable declared as var can only be used locally , dynamic variables can be passed in as params to function (function signature can define a param as dynamic but not var). with dynamic the resolution of the properties happens at runtime and thats not the case with var which means at compile time any variable declared as dynamic can call a method which may or maynot exist and so the compiler would not throw an error. Type casting with var not possible but with dynamic its possible (you can cast an object as dynamic but not as var).
阿伦Vijayraghavan
用var声明的变量的类型是由编译器决定的,它是指定类型名称的快捷方式,仅此而已。
然而动态是在运行时确定的,编译器不知道实际的类型,所有对该变量的方法/字段/属性访问都将在运行时计算出来。