最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

再議C#中的裝箱與拆箱的問題詳解

 更新時間:2013年05月18日 15:53:42   作者:  
本篇文章再次介紹了C#中的裝箱與拆箱,這次們看下使用泛型和不使用泛型引發(fā)裝箱拆箱的情況
上一篇寫了一下裝箱拆箱的定義和IL分析,這一篇我們看下使用泛型和不使用泛型引發(fā)裝箱拆箱的情況
1.使用非泛型集合時引發(fā)的裝箱和拆箱操作
看下面的一段代碼:
復(fù)制代碼 代碼如下:

var array = new ArrayList();
array.Add(1);
array.Add(2);

foreach (int value in array)
{
Console.WriteLine(“value is {0}”,value);
}

代碼聲明了一個ArrayList對象,向ArrayList中添加兩個數(shù)字1,2;然后使用foreach將ArrayList中的元素打印到控制臺。
在這個過程中會發(fā)生兩次裝箱操作和兩次拆箱操作,在向ArrayList中添加int類型元素時會發(fā)生裝箱,在使用foreach枚舉ArrayList中的int類型元素時會發(fā)生拆箱操作,將object類型轉(zhuǎn)換成int類型,在執(zhí)行到Console.WriteLine時,還會執(zhí)行兩次的裝箱操作;這一段代碼執(zhí)行了6次的裝箱和拆箱操作;如果ArrayList的元素個數(shù)很多,執(zhí)行裝箱拆箱的操作會更多。
你可以通過使用ILSpy之類的工具查看IL代碼的box,unbox指令查看裝箱和拆箱的過程
2.使用泛型集合的情況
請看如下代碼:
復(fù)制代碼 代碼如下:

var list = new List<int>();
list.Add(1);
list.Add(2);

foreach (int value in list)
{
Console.WriteLine("value is {0}", value);
}

代碼和1中的代碼的差別在于集合的類型使用了泛型的List,而非ArrayList;我們同樣可以通過查看IL代碼查看裝箱拆箱的情況,上述代碼只會在Console.WriteLine()方法時執(zhí)行2次裝箱操作,不需要拆箱操作。
可以看出泛型可以避免裝箱拆箱帶來的不必要的性能消耗;當(dāng)然泛型的好處不止于此,泛型還可以增加程序的可讀性,使程序更容易被復(fù)用等等。
本文使用的C#代碼如下:
復(fù)制代碼 代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;

namespace boxOrUnbox
{
    class Program
    {
        static void Main(string[] args)
        {
            //do nothing
        }

        static void Box()
        {
            object objValue = 9;
        }

        static void Unbox()
        {
            object objValue = 4;
            int value = (int)objValue;
        }

        static void LookatArrayList()
        {
            var array = new ArrayList();
            array.Add(1);
            array.Add(2);

            foreach (int value in array)
            {
                Console.WriteLine("value is {0}", value);
            }
        }

        static void LookatGenericList()
        {
            var list = new List<int>();
            list.Add(1);
            list.Add(2);

            foreach (int value in list)
            {
                Console.WriteLine("value is {0}", value);
            }
        }
    }
}

C#的IL代碼如下:
復(fù)制代碼 代碼如下:

.class private auto ansi beforefieldinit boxOrUnbox.Program
    extends [mscorlib]System.Object
{
    // Methods
    .method private hidebysig static
        void Main (
            string[] args
        ) cil managed
    {
        // Method begins at RVA 0x2050
        // Code size 2 (0x2)
        .maxstack 8
        .entrypoint

        IL_0000: nop
        IL_0001: ret
    } // end of method Program::Main

    .method private hidebysig static
        void Box () cil managed
    {
        // Method begins at RVA 0x2054
        // Code size 10 (0xa)
        .maxstack 1
        .locals init (
            [0] object objValue
        )

        IL_0000: nop
        IL_0001: ldc.i4.s 9
        IL_0003: box [mscorlib]System.Int32
        IL_0008: stloc.0
        IL_0009: ret
    } // end of method Program::Box

    .method private hidebysig static
        void Unbox () cil managed
    {
        // Method begins at RVA 0x206c
        // Code size 16 (0x10)
        .maxstack 1
        .locals init (
            [0] object objValue,
            [1] int32 'value'
        )

        IL_0000: nop
        IL_0001: ldc.i4.4
        IL_0002: box [mscorlib]System.Int32
        IL_0007: stloc.0
        IL_0008: ldloc.0
        IL_0009: unbox.any [mscorlib]System.Int32
        IL_000e: stloc.1
        IL_000f: ret
    } // end of method Program::Unbox

    .method private hidebysig static
        void LookatArrayList () cil managed
    {
        // Method begins at RVA 0x2088
        // Code size 114 (0x72)
        .maxstack 2
        .locals init (
            [0] class [mscorlib]System.Collections.ArrayList 'array',
            [1] int32 'value',
            [2] class [mscorlib]System.Collections.IEnumerator CS$5$0000,
            [3] bool CS$4$0001,
            [4] class [mscorlib]System.IDisposable CS$0$0002
        )

        IL_0000: nop
        IL_0001: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()
        IL_0006: stloc.0
        IL_0007: ldloc.0
        IL_0008: ldc.i4.1
        IL_0009: box [mscorlib]System.Int32
        IL_000e: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
        IL_0013: pop
        IL_0014: ldloc.0
        IL_0015: ldc.i4.2
        IL_0016: box [mscorlib]System.Int32
        IL_001b: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
        IL_0020: pop
        IL_0021: nop
        IL_0022: ldloc.0
        IL_0023: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.ArrayList::GetEnumerator()
        IL_0028: stloc.2
        .try
        {
            IL_0029: br.s IL_004a
            // loop start (head: IL_004a)
                IL_002b: ldloc.2
                IL_002c: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()
                IL_0031: unbox.any [mscorlib]System.Int32
                IL_0036: stloc.1
                IL_0037: nop
                IL_0038: ldstr "value is {0}"
                IL_003d: ldloc.1
                IL_003e: box [mscorlib]System.Int32
                IL_0043: call void [mscorlib]System.Console::WriteLine(string, object)
                IL_0048: nop
                IL_0049: nop

                IL_004a: ldloc.2
                IL_004b: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
                IL_0050: stloc.3
                IL_0051: ldloc.3
                IL_0052: brtrue.s IL_002b
            // end loop

            IL_0054: leave.s IL_0070
        } // end .try
        finally
        {
            IL_0056: ldloc.2
            IL_0057: isinst [mscorlib]System.IDisposable
            IL_005c: stloc.s CS$0$0002
            IL_005e: ldloc.s CS$0$0002
            IL_0060: ldnull
            IL_0061: ceq
            IL_0063: stloc.3
            IL_0064: ldloc.3
            IL_0065: brtrue.s IL_006f

            IL_0067: ldloc.s CS$0$0002
            IL_0069: callvirt instance void [mscorlib]System.IDisposable::Dispose()
            IL_006e: nop

            IL_006f: endfinally
        } // end handler

        IL_0070: nop
        IL_0071: ret
    } // end of method Program::LookatArrayList

    .method private hidebysig static
        void LookatGenericList () cil managed
    {
        // Method begins at RVA 0x2118
        // Code size 90 (0x5a)
        .maxstack 2
        .locals init (
            [0] class [mscorlib]System.Collections.Generic.List`1<int32> list,
            [1] int32 'value',
            [2] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32> CS$5$0000,
            [3] bool CS$4$0001
        )

        IL_0000: nop
        IL_0001: newobj instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
        IL_0006: stloc.0
        IL_0007: ldloc.0
        IL_0008: ldc.i4.1
        IL_0009: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
        IL_000e: nop
        IL_000f: ldloc.0
        IL_0010: ldc.i4.2
        IL_0011: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
        IL_0016: nop
        IL_0017: nop
        IL_0018: ldloc.0
        IL_0019: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<int32>::GetEnumerator()
        IL_001e: stloc.2
        .try
        {
            IL_001f: br.s IL_003c
            // loop start (head: IL_003c)
                IL_0021: ldloca.s CS$5$0000
                IL_0023: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::get_Current()
                IL_0028: stloc.1
                IL_0029: nop
                IL_002a: ldstr "value is {0}"
                IL_002f: ldloc.1
                IL_0030: box [mscorlib]System.Int32
                IL_0035: call void [mscorlib]System.Console::WriteLine(string, object)
                IL_003a: nop
                IL_003b: nop

                IL_003c: ldloca.s CS$5$0000
                IL_003e: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::MoveNext()
                IL_0043: stloc.3
                IL_0044: ldloc.3
                IL_0045: brtrue.s IL_0021
            // end loop

            IL_0047: leave.s IL_0058
        } // end .try
        finally
        {
            IL_0049: ldloca.s CS$5$0000
            IL_004b: constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>
            IL_0051: callvirt instance void [mscorlib]System.IDisposable::Dispose()
            IL_0056: nop
            IL_0057: endfinally
        } // end handler

        IL_0058: nop
        IL_0059: ret
    } // end of method Program::LookatGenericList

    .method public hidebysig specialname rtspecialname
        instance void .ctor () cil managed
    {
        // Method begins at RVA 0x2190
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: ret
    } // end of method Program::.ctor

} // end of class boxOrUnbox.Program

相關(guān)文章

  • C#打印日志的方法總結(jié)

    C#打印日志的方法總結(jié)

    在本篇文章里小編給大家整理了關(guān)于C#如何打印日志的技巧總結(jié),需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • c#基于WinForm的Socket實現(xiàn)簡單的聊天室 IM

    c#基于WinForm的Socket實現(xiàn)簡單的聊天室 IM

    這篇文章主要介紹了c#基于WinForm的Socket實現(xiàn)簡單的聊天室 IM的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-05-05
  • c# dynamic的使用詳解

    c# dynamic的使用詳解

    這篇文章主要介紹了c# dynamic的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題

    C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題

    這篇文章主要介紹了C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Unity實現(xiàn)手機搖一搖震動

    Unity實現(xiàn)手機搖一搖震動

    這篇文章主要為大家詳細(xì)介紹了untiy實現(xiàn)手機搖一搖震動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C#圖片上傳效果實例分析

    C#圖片上傳效果實例分析

    這篇文章主要介紹了C#圖片上傳效果實現(xiàn)方法,可實現(xiàn)圖片上傳效果預(yù)覽功能,需要的朋友可以參考下
    2015-06-06
  • http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實不準(zhǔn)確、不安全

    http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實不準(zhǔn)確、不安全

    圖片上傳常用的類型判斷方法有這么幾種---截取擴展名、獲取文件ContentType (MIME) 、讀取byte來判斷(這個什么叫法來著?)。下面由腳本之家小編跟大家分享圖片上傳安全性問題,感興趣的朋友一起看看吧
    2015-09-09
  • C#統(tǒng)計C、C++及C#程序代碼行數(shù)的方法

    C#統(tǒng)計C、C++及C#程序代碼行數(shù)的方法

    這篇文章主要介紹了C#統(tǒng)計C、C++及C#程序代碼行數(shù)的方法,較為詳細(xì)的分析了C#統(tǒng)計文本文件的原理與相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • .net從服務(wù)器下載文件中文名亂碼解決方案

    .net從服務(wù)器下載文件中文名亂碼解決方案

    這篇文章主要給大家介紹.net中從服務(wù)器下載文件中文名亂碼的解決方案,有需要的朋友可以參考下
    2015-08-08
  • C#中緩存System.Web.Caching用法總結(jié)

    C#中緩存System.Web.Caching用法總結(jié)

    本文詳細(xì)講解了C#中緩存System.Web.Caching的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04

最新評論

徐汇区| 滁州市| 林周县| 永泰县| 全南县| 敖汉旗| 阳春市| 台湾省| 定陶县| 调兵山市| 宁安市| 綦江县| 新和县| 武冈市| 洪雅县| 阿克陶县| 新余市| 高邮市| 万载县| 吉林市| 荃湾区| 黔西县| 湘阴县| 姜堰市| 山丹县| 布拖县| 靖安县| 芜湖县| 贵阳市| 新郑市| 两当县| 云龙县| 巴南区| 平顺县| 天等县| 静乐县| 石门县| 紫云| 阜南县| 金阳县| 山阳县|