Home > Backend Development > C#.Net Tutorial > C# implements a simple example of boxing and unboxing operations

C# implements a simple example of boxing and unboxing operations

高洛峰
Release: 2017-01-24 14:22:27
Original
1513 people have browsed it

This article uses a simple example to describe the implementation method of C# boxing and unboxing operations. Simply put, boxing is to convert the value type into a reference type; unboxing is to convert the reference type into a value type, which involves the stack and Anyone who has learned C# should know about the use of the heap, so I won’t be too embarrassed here. This example code is also for C# novices and is very simple.

The specific implementation code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnBoxing
{
  class Program
  {
    static void Main(string[] args)
    {
      int i = 123;//声明一个int类型的变量i,并初始化为123
      object obj = i; //执行装箱操作
      Console.WriteLine("装箱操作:值为{0},装箱之后对象为{1}", i, obj);
      int j = (int)obj;//执行拆箱操作
      Console.WriteLine("拆箱操作:装箱对象为{0},值为{1}", obj, j);
      Console.ReadLine();
    }
  }
}
Copy after login

For more C# simple examples of boxing and unboxing operations, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template