C# 何时使用装箱和拆箱?

C# 何时使用装箱和拆箱?,c#,object,boxing,C#,Object,Boxing,我知道装箱将值类型转换为对象和取消装箱将对象转换为值类型。 例如: 但是当我在现实生活中使用它时(当我写一些代码时)? 我从不将Int赋值给对象(或者我认为是这样,我没有明确地编写它)装箱/取消装箱对于可能有一组不同的东西的情况很有用,例如,其中一些是原语,另一些是对象类型,如列表。事实上,这就是他们在文档页面中给出的示例,如果您搜索“msdn装箱”: //列出示例。 //创建对象列表以保存异类集合 //元素的组合。 List mixedList=新列表(); //将字符串元素添加到列表中。 添

我知道装箱将值类型转换为对象和取消装箱将对象转换为值类型。 例如:

但是当我在现实生活中使用它时(当我写一些代码时)?
我从不将Int赋值给对象(或者我认为是这样,我没有明确地编写它)

装箱/取消装箱对于可能有一组不同的东西的情况很有用,例如,其中一些是原语,另一些是对象类型,如
列表。事实上,这就是他们在文档页面中给出的示例,如果您搜索“msdn装箱”:

//列出示例。
//创建对象列表以保存异类集合
//元素的组合。
List mixedList=新列表();
//将字符串元素添加到列表中。
添加(“第一组:”);
//向列表中添加一些整数。
对于(int j=1;j<5;j++)
{
//将鼠标指针停留在j上,以验证是否正在添加
//对象列表的整数。当
//将j添加到mixedList。
混合列表。添加(j);
}
//添加另一个字符串和更多整数。
添加(“第二组:”);
对于(int j=5;j<10;j++)
{
混合列表。添加(j);
}
//显示列表中的元素。通过以下方式声明循环变量:
//使用var,以便编译器分配其类型。
foreach(混合列表中的变量项)
{
//将鼠标指针停留在项目上,以验证元素
//mixedList的一部分是对象。
控制台写入线(项目);
}
//下面的循环对第一组已装箱数据的平方求和
//混合列表中的整数。列表元素是对象,不能使用
//被乘以或加到总和上,直到它们被解除。这个
//必须显式地取消装箱。
var总和=0;
对于(var j=1;j<5;j++)
{
//以下语句导致编译器错误:运算符
//“*”不能应用于“object”类型的操作数,并且
//“对象”。
//sum+=mixedList[j]*mixedList[j]);
//列表元素解除绑定后,计算将执行
//不会导致编译器错误。
总和+=(int)混合列表[j]*(int)混合列表[j];
}
//显示的总和为30,即1+4+9+16之和。
控制台写入线(“总和:+总和”);
//输出:
//答案是正确的
//第一组:
// 1 
// 2 
// 3 
// 4 
//第二组:
// 5 
// 6 
// 7 
// 8 
// 9 
//总数:30

这里有一个例子:另一个例子是使用旧集合(在ArrayList等泛型之前),例如,当您将不同类型的变量传递给方法时,通常使用装箱。
String.Format
takes参数是对象,因此可以混合放入字符串中的数据类型:
String s=String.Format({1}是{0}),42,“answer”)
String.Concat
可以将一组对象转换为字符串并concatentate:
String s=String.Contcat(“一”,二,“三”,四)
当使用
+
运算符进行连接时,也会发生同样的情况,编译器将生成对
String.Concat的调用,以执行以下操作:
String s=“one”+2+“three”+4
int num = 1;
Object obj = num;      //boxing
int num2 = (int)obj    //unboxing
// List example. 
// Create a list of objects to hold a heterogeneous collection  
// of elements.
List<object> mixedList = new List<object>();

// Add a string element to the list. 
mixedList.Add("First Group:");

// Add some integers to the list.  
for (int j = 1; j < 5; j++)
{
    // Rest the mouse pointer over j to verify that you are adding 
    // an int to a list of objects. Each element j is boxed when  
    // you add j to mixedList.
    mixedList.Add(j);
}

// Add another string and more integers.
mixedList.Add("Second Group:");
for (int j = 5; j < 10; j++)
{
    mixedList.Add(j);
}

// Display the elements in the list. Declare the loop variable by  
// using var, so that the compiler assigns its type. 
foreach (var item in mixedList)
{
    // Rest the mouse pointer over item to verify that the elements 
    // of mixedList are objects.
    Console.WriteLine(item);
}

// The following loop sums the squares of the first group of boxed 
// integers in mixedList. The list elements are objects, and cannot 
// be multiplied or added to the sum until they are unboxed. The 
// unboxing must be done explicitly. 
var sum = 0;
for (var j = 1; j < 5; j++)
{
    // The following statement causes a compiler error: Operator  
    // '*' cannot be applied to operands of type 'object' and 
    // 'object'.  
    //sum += mixedList[j] * mixedList[j]); 

    // After the list elements are unboxed, the computation does  
    // not cause a compiler error.
    sum += (int)mixedList[j] * (int)mixedList[j];
}

// The sum displayed is 30, the sum of 1 + 4 + 9 + 16.
Console.WriteLine("Sum: " + sum);

// Output: 
// Answer42True 
// First Group: 
// 1 
// 2 
// 3 
// 4 
// Second Group: 
// 5 
// 6 
// 7 
// 8 
// 9 
// Sum: 30