📜  C#中var和dynamic的区别

📅  最后修改于: 2021-09-12 10:59:17             🧑  作者: Mango

隐式类型的局部变量——var是那些在没有明确指定.NET 类型的情况下声明的变量。在隐式类型变量中,编译器在编译时根据用于初始化变量的值自动推导出变量的类型。 C# 3.0 中引入了隐式类型变量的概念。隐式类型变量不是为了替代普通变量声明而设计的,它旨在处理一些特殊情况,如LINQ (语言集成查询)。

例子:

// C# program to illustrate the concept
// of the implicitly typed variable
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing
        // implicitly typed variables
        // Using var keyword
        var a = 'f';
        var b = "GeeksforGeeks";
        var c = 30.67d;
        var d = false;
        var e = 54544;
  
        // Display the type
        Console.WriteLine("Type of 'a' is : {0} ", a.GetType());
  
        Console.WriteLine("Type of 'b' is : {0} ", b.GetType());
  
        Console.WriteLine("Type of 'c' is : {0} ", c.GetType());
  
        Console.WriteLine("Type of 'd' is : {0} ", d.GetType());
  
        Console.WriteLine("Type of 'e' is : {0} ", e.GetType());
    }
}

输出:

Type of 'a' is : System.Char 
Type of 'b' is : System.String 
Type of 'c' is : System.Double 
Type of 'd' is : System.Boolean 
Type of 'e' is : System.Int32 

C# 4.0 中,引入了一种称为动态类型的新类型。它用于避免编译时类型检查。编译器在编译时不检查动态类型变量的类型,而是在运行时获取类型。动态类型变量是使用 dynamic 关键字创建的。

例子:

// C# program to illustrate how to get the
// actual type of the dynamic type variable
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Dynamic variables
        dynamic val1 = "GeeksforGeeks";
        dynamic val2 = 3234;
        dynamic val3 = 32.55;
        dynamic val4 = true;
  
        // Get the actual type of
        // dynamic variables
        // Using GetType() method
        Console.WriteLine("Get the actual type of val1: {0}",
                                  val1.GetType().ToString());
  
        Console.WriteLine("Get the actual type of val2: {0}",
                                  val2.GetType().ToString());
  
        Console.WriteLine("Get the actual type of val3: {0}",
                                  val3.GetType().ToString());
  
        Console.WriteLine("Get the actual type of val4: {0}",
                                  val4.GetType().ToString());
    }
}

输出:

Get the actual type of val1: System.String
Get the actual type of val2: System.Int32
Get the actual type of val3: System.Double
Get the actual type of val4: System.Boolean

以下是 C# 中 var 和 dynamic 关键字之间的一些区别:

Var Dynamic
It is introduced in C# 3.0. It is introduced in C# 4.0
The variables are declared using var keyword are statically typed. The variables are declared using dynamic keyword are dynamically typed.
The type of the variable is decided by the compiler at compile time. The type of the variable is decided by the compiler at run time.
The variable of this type should be initialized at the time of declaration. So that the compiler will decide the type of the variable according to the value it initialized. The variable of this type need not be initialized at the time of declaration. Because the compiler does not know the type of the variable at compile time.
If the variable does not initialized it throw an error. If the variable does not initialized it will not throw an error.
It support intelliSense in visual studio. It does not support intelliSense in visual studio
var myvalue = 10; // statement 1
myvalue = “GeeksforGeeks”; // statement 2
Here the compiler will throw an error because the compiler has already decided the type of the myvalue variable using statement 1 that is an integer type. When you try to assign a string to myvalue variable, then the compiler will give an error because it violating safety rule type.
dynamic myvalue = 10; // statement 1
myvalue = “GeeksforGeeks”; // statement 2
Here, the compiler will not throw an error though the type of the myvalue is an integer. When you assign a string to myvalue it recreates the type of the myvalue and accepts string without any error.
It cannot be used for properties or returning values from the function. It can only used as a local variable in function. It can be used for properties or returning values from the function.