سلام بر همه برنامه نویسان ! در این مطلب کد Boxing و Unboxing در سی شارپ رو براتون آماده کرده ام.امیدوارم که از این کد سی شارپ نهایت استفاده رو ببرین.
//Boxing--> It is a process of converting a variable from value type to reference type. it
//supports two types,
//1. Implicit boxing
//2. Explicit Boxing
//Boxing is 20 times costlier than normal Initialization.
//UnBoxing--> Converting a variable from Reference type to value type. it supports only One type,
//1. Explicit Boxing
//UnBoxing is 4 times Costlier than normal Initialization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace box
{
class Program
{
static void Main(string[] args)
{
int i = 10;
object O = i; //Implicit Boxing
object X = (object) i; // Explicit Boxing
int j = (int) O; // Explicit Unboxing
Console.WriteLine("value of i is:-" + i);
Console.WriteLine("value of O after Implicit Boxing is :-" + O);
Console.WriteLine("value of X after Explicit Boxing is :-" + X);
Console.WriteLine("value of j after Explicit UnBoxing is :-" + j);
Console.Read();
}
}
}
//supports two types,
//1. Implicit boxing
//2. Explicit Boxing
//Boxing is 20 times costlier than normal Initialization.
//UnBoxing--> Converting a variable from Reference type to value type. it supports only One type,
//1. Explicit Boxing
//UnBoxing is 4 times Costlier than normal Initialization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace box
{
class Program
{
static void Main(string[] args)
{
int i = 10;
object O = i; //Implicit Boxing
object X = (object) i; // Explicit Boxing
int j = (int) O; // Explicit Unboxing
Console.WriteLine("value of i is:-" + i);
Console.WriteLine("value of O after Implicit Boxing is :-" + O);
Console.WriteLine("value of X after Explicit Boxing is :-" + X);
Console.WriteLine("value of j after Explicit UnBoxing is :-" + j);
Console.Read();
}
}
}
موفق و پیروز باشید…