-1

I know boxing convert value type to object and unboxing convert object to a value type. Example:

int num = 1;
Object obj = num;      //boxing
int num2 = (int)obj    //unboxing

But when I use it in the real life (when I write some code)? I never assign Int into Object (or that's what I think so, I didn't write it explicitly)

Avi Cohen
  • 55
  • 5
  • Here's one example: http://stackoverflow.com/questions/14561382/why-net-has-no-generic-version-of-thread-start Another example is using old collections(prior to generics like ArrayList) – Amir Popovich Dec 14 '14 at 16:23
  • Boxing is for example commonly used when you pass variables of different types to a method. The `String.Format` takes parameters are objects, so that you can mix data types that are put in the string: `string s = String.Format("The {1} is {0}", 42, "answer");` The `String.Concat` can take a collection of objects to turn into strings and concatentate: `string s = String.Contcat("one", 2, "three", 4);` The same happens when you concatenate using the `+` operator, the compiler will generate a call to `String.Concat` to do the work: `string s = "one" + 2 + "three" + 4;` – Guffa Dec 14 '14 at 16:26

1 Answers1

1

Boxing/unboxing is useful for situations where you may have, for instance, a collection of disparate things, some of which are primitives and others of which are object types, such as a List<object>. Indeed, that's the example they give in the Boxing and Unboxing documentation page that comes up if you search for "msdn boxing:"

// 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
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875