我已经阅读了大量关于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意味着应用动态类型检查(后期绑定)。在代码方面,考虑以下内容:

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自己测试它。

我会解释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是静态类型的——编译器和运行时知道它的类型——它们只是为你节省了一些输入…以下是100%相同的:

var s = "abc";
Console.WriteLine(s.Length);

and

string s = "abc";
Console.WriteLine(s.Length);

所发生的一切只是编译器发现s必须是一个字符串(来自初始化式)。在这两种情况下,它都知道(在IL中)s.l end意味着(实例)字符串。长度属性。

动态是一种非常不同的野兽;它与object最相似,但具有动态调度:

dynamic s = "abc";
Console.WriteLine(s.Length);

这里,s的类型是动态的。它不知道字符串。长度,因为它在编译时不知道s的任何信息。例如,下面的代码也可以编译(但不能运行):

dynamic s = "abc";
Console.WriteLine(s.FlibbleBananaSnowball);

在运行时(仅),它将检查flibb黎巴嫩asnowball属性-未能找到它,并爆炸在一个火花淋浴。

使用dynamic,属性/方法/操作符等在运行时根据实际对象解析。非常方便地与COM(可以具有仅运行时属性)、DLR或其他动态系统(如javascript)通信。

用var声明的变量是隐式的,但是是静态类型的。使用dynamic声明的变量是动态类型的。CLR添加此功能是为了支持Ruby和Python等动态语言。

我应该补充一点,这意味着动态声明在运行时解析,var声明在编译时解析。

一个很大的不同——你可以有一个动态返回类型。

dynamic Foo(int x)
{
    dynamic result;

    if (x < 5)
      result = x;
    else
      result = x.ToString();

    return result;
}