C # Grammar Learning Boxing (box) and unboxing (unbox)

Source: Internet
Author: User

/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/4
* Time: 上午 12:10
*
*/
using System;
class Test
{
static void Main()
{
int i=123;//第一次裝箱
object o=i;
i=456;
//這里由於使用了+號也就會調用字符串的
//String.Concat(Object,Object,Object);
//這也就讓下面的都需要轉換成Object類型
Console.WriteLine(i //第二次裝箱
+ ", " +
(int)o);//這里先發生一次拆箱,再接著發生第三次裝箱
}
}
//問題:上面的程序會進行幾次裝箱和拆箱.
//3次裝箱,1次拆箱.
//這里也就告訴我們在寫程序的時候不注意就進行裝箱和拆箱的操作
//為了提高程序的性能我們可以裝輸出語句改為下面的:
//Console.WriteLine(i + ", " + o);輸出結果一致.這時候進行了兩次裝箱
//Console.WriteLine(i.ToString() + ", " + o);輸出結果一致.這時候進行了一次裝箱

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.