Dig a series of things that are not commonly used in C # (4) -- GetHashCode, ExpandoObject

Source: Internet
Author: User

 

In this article, we will continue to share the two interesting Methods: GetHashCode and ExpandoObject.

1. GetHashCode

It can be seen from MSDN that it is used as a hash function of a specific type. That is to say, any object instance will have a HashCode of the int32 type and be stored in the FCL

In HashCollection, let's look at an example:

We can see that the hashcode of the two class instances is different, indicating that the two instances are not the same reference, and different hashcode is generated. Using this feature, do we have

Can we generate some random numbers?

1: generate with random in the for loop.

 1         static void Main(string[] args) 2         { 3             var list = new List<int>(); 4  5             for (int i = 0; i < byte.MaxValue; i++) 6             { 7                 list.Add(new Random().Next(0, byte.MaxValue)); 8             } 9 10             list.ForEach((i) =>11             {12                 Console.WriteLine(i);13             });14 15             Console.Read();16         }

We know that Random is pseudo-Random, so a series of numbers are repeated. What should I do if I really want a Random number? At this time, you can try HashCode.

2: hashcode in the for Loop

 1         static void Main(string[] args) 2         { 3             var list = new List<int>(); 4  5             for (int i = 0; i < byte.MaxValue; i++) 6             { 7                 list.Add(new Random().GetHashCode()); 8             } 9 10             list.ForEach((i) =>11             {12                 Console.WriteLine(i);13             });14 15             Console.Read();16         }

However, we can see that we are constantly pushing to the managed service, so there is still a certain performance overhead for GC.

 

Ii. ExpandoObject

We know that php, asp, and js are both explanatory languages, saving compilation troubles. I used php for six months last year and then returned to C #, then it will be particularly uncomfortable with C # compilation.

Sometimes it takes more than 10 minutes to compile a solution of more than one hundred dll files. The Weekly release date is released to the production environment through automated tools.

Environment, all need to be re-compiled, resulting in a lot of time spent on compilation, but after C #4.0, we use dynamic features, C # can also be written as js.

For example, in the ExpandoObject class, we can dynamically append some attributes and methods to ExpandoObject through later binding, which is very interesting. However, note that

Once dynamic is enabled, the compiler does not recognize the code and implements the compilation-free function.

 1         static void Main(string[] args) 2         { 3             dynamic obj = new System.Dynamic.ExpandoObject(); 4              5             obj.Name = "hxc"; 6  7             obj.Age = 20; 8  9             obj.Call = new Action(() => { Console.WriteLine("call me!!!"); });10 11             obj.Call();12 13             Console.Read();14         }

 

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.