C # type conversion is and as and performance traps

Source: Internet
Author: User
1. Before C #2.0, as can only be used for reference types. After C #2.0, it can also be used as an empty type. The result is a null value or a meaningful value.
Example:
 1   static void Main(string[] args) 2         { 3             PrintValueInt32(5); 4             PrintValueInt32("some thing!"); 5     } 6      7       static void PrintValueInt32(object o) 8         { 9             int? nullable = o as int?;10             Console.WriteLine(nullable.HasValue ? nullable.Value.ToString() : "null");11         }
Output:

 

In this way, you can safely convert any reference to a value in one step. In C #1.0, you can only use the is operator first, and then lightweight conversion. This makes the CLR perform two type checks, which is obviously not elegant enough.

 

2. I always thought that a single check would be faster than two. Is that true? Let's test it.

 

1 static void main (string [] ARGs) 2 {3 list <Object> listobj = new list <Object> (); 4 List <Int?> List = new list <Int?> (); 5 6 for (INT I = 0; I <100000; I ++) 7 {8 listobj. add (I); 9} 10 11 stopwatch watch = new stopwatch (); // test time Class 12 watch. start (); 13 foreach (VAR item in listobj) 14 {15 if (item is int) // is and strong type conversion 16 {17 list. add (INT) item); 18} 19} 20 watch. stop (); 21 console. writeline ("is check and strong type conversion time: {0}", watch. elapsed); 22 23 watch. reset (); 24 watch. start (); 25 foreach (VAR item in listobj) 26 {27 Int? Temp = item as Int ?; // As conversion and null value determination, 28 If (temp. hasvalue) // temp is also available here! = NULL to judge 29 {// because temp is a void type, hasvalue, value, and so on can be used to operate on the void type 30 List. add (temp); 31} 32} 33 watch. stop (); 34 console. writeline ("as conversion time: {0}", watch. elapsed); 35 console. readkey (); 36}

Output:

The output results show that the use of the is check and strong type conversion time is much less than the as conversion time. Exercise caution when converting between referenced types and value types frequently to avoid falling into the performance trap.

3. Use of null merge operators.

The null merge operator is introduced in C #2.0. First ?? Second:

(1) Evaluate first.

(2) If first is not null, the value of the entire expression is the value of first.

(3) Otherwise, calculate the value of second as the value of the entire expression.

Sample Code 1: when the first parameter is not null, the value of the entire expression is the first parameter no matter whether the second parameter is null or not:

 

            int? first = 1;            int? second = 4;            int? temp = first ?? second;            Console.WriteLine("{0}", (temp.HasValue ? temp.Value.ToString() : "null"));

Output: (even if second is not empty, the first value is output because first is not empty)

Code Example 2: If the first parameter is null, the value of second is the value of the entire expression.

 

1             int? first = null;2             int? second = 4;3             int? temp = first ?? second;4             Console.WriteLine("{0}", (temp.HasValue ? temp.Value.ToString() : "null"));

Output:

For more information, see Jon steek.

C # type conversion is and as and performance traps

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.