Differences between Var and Dynamic and VarDynamic

Source: Internet
Author: User

Differences between Var and Dynamic and VarDynamic

1. Differences between var and dynamic

Many keywords in C # are easy to confuse. var and dynamic are one of them, but they are actually essentially different. Var has determined the type in the compilation phase. during initialization, the initialization value must be provided, while dynamic does not. It determines the type only at runtime.

 

1 static void Main (string [] args) 2 {3 // var has determined the type 4 var number = 3 in the compilation phase; 5 6 // dynamic does not perform any type check during compilation. Instead, it puts the type check into the runtime 7 dynamic dyn = 3; 8 9 dyn = "hello world "; 10 11 // runtime eror12 // The string does not have the "fn nonexistent method" method, but the syntax error 13 var s = dyn is not prompted when the syntax is checked. fn does not exist (); 14 15 Console. readKey (); 16}

 

 

 

 

 

2. Use of the ExpandoObject type

 

When we need to create some temporary variables for some purpose, we can do the following:

 

 

1 var a = new { Name = "Paul Huang", Age = 24 };2 Console.WriteLine("Name is {0},Age is {1}", a.Name, a.Age);

 

 

 

Now we want to introduce a new type of ExpandoObject, which is designed for dynamic behavior. Its usage is as follows:

 

1 static void Main(string[] args)2 {3     dynamic a = new ExpandoObject();  4     a.Name = "Paul Huang";  5     a.Age = 24; 6     Console.WriteLine("Name is {0},Age is {1}", a.Name, a.Age);7  }

 

 

 

In fact, the ExpandoObject class explicitly implements the IDictionary <string, object> interface, so we can know that it actually uses a dictionary to store the value of dynamic assignment, the type of the key is string, indicating the property name; the type of the value is object, indicating any type.

Let's output the dictionary data in it:

 

 

1 IDictionary<string, object> dic = a as IDictionary<string, object>;2 foreach (var pv in dic)3 {4     Console.WriteLine("Key = {0} , Value = {1}", pv.Key, pv.Value);5 }

 

 

 

Therefore, no matter how you set the attributes dynamically, it can be parsed. This is the reason why a dictionary is used to access data.

 

3. Application of dynamic in reflection

 

Because dynamic checks the type only at run time, sometimes an error occurs, so it must be used properly. When will it be more convenient to use it? We first define a Person class, and then use reflection to dynamically call the Talk method:

 

 

1 class Person2 {3     public void Talk(string msg)4     {5         Console.WriteLine(msg);6     }7 }

 

 

 

 

 1 class Program 2 { 3     static void Main(string[] args) 4     { 5         Type t = typeof(Person); 6         Object obj = Activator.CreateInstance(t); 7         t.InvokeMember("Talk", BindingFlags.InvokeMethod, null, obj, new object[] { "hello world" }); 8         Console.ReadKey(); 9     }10 }

 

 

 

In reflection, traditional method calls are often complicated, while dyanmic is simplified and intuitive:

 

 1 class Program 2 { 3     static void Main(string[] args) 4   { 5           Type t = typeof(Person); 6           dynamic obj = Activator.CreateInstance(t); 7           obj.Talk("hello world"); 8      Console.ReadKey(); 9   }10 }

 

 

 

 

Reprinted link: https://www.cnblogs.com/JustYong/p/5113197.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.