Questions tagged [boxing]

Boxing is when a value type is wrapped in a reference-type wrapper for the purposes of using it when polymorphism (conversion to Object or an interface) is required.

In Java and some similar languages there is a differentiation between primitives (or value types) and reference types. The primitive types work as simple values and, when used as a parameter to methods, do not change the original operand. Reference types on the other hand can be modified when passed into methods and have associated classes that define objects of that type (whereas primitives are more "built-in"). When viewed from the perspective of language such as C or C++, instances of reference types can be thought of as pointers that are automatically de-referenced.

Boxing occurs when a primitive value type is used in place of a corresponding reference type. In Java, for example, if a value of type int is used where an Integer is expected, the compiler will automatically wrap the value in an Integer object.

588 questions
26
votes
7 answers

Boxing and Unboxing in String.Format(...) ... is the following rationalized?

I was doing some reading regarding boxing/unboxing, and it turns out that if you do an ordinary String.Format() where you have a value type in your list of object[] arguments, it will cause a boxing operation. For instance, if you're trying to print…
Carlos
  • 5,991
  • 6
  • 43
  • 82
25
votes
4 answers

Does calling a method on a value type result in boxing in .NET?

I was just participating in Stack Overflow question Is everything in .NET an object?. And one poster (in comments of accepted answer) seemed to think that performing a method call on a value type resulted in boxing. He pointed me to Boxing and…
Quibblesome
  • 25,225
  • 10
  • 61
  • 100
25
votes
6 answers

Why do structs need to be boxed?

In C#, any user-defined struct is automatically a subclass of System.Struct System.ValueType and System.Struct System.ValueType is a subclass of System.Object. But when we assign some struct to object-type reference it gets boxed. For…
Red Hyena
  • 2,988
  • 5
  • 25
  • 24
25
votes
6 answers

Compiler error : reference to call ambiguous

Case 1 static void call(Integer i) { System.out.println("hi" + i); } static void call(int i) { System.out.println("hello" + i); } public static void main(String... args) { call(10); } Output of Case 1 : hello10 Case 2 static void…
Ravi
  • 30,829
  • 42
  • 119
  • 173
24
votes
2 answers

Does int.class equal Integer.class or Integer.TYPE in Java?

Let's imagine one retrieves the declaring type of a Field using reflection. Which of the following tests will correctly indicate whether one is dealing with an int or an Integer? Field f = ... Class c = f.getDeclaringClass(); boolean…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
23
votes
1 answer

Boxing and unboxing when using out and ref parameters

Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType?
brain_pusher
  • 1,507
  • 3
  • 21
  • 31
20
votes
3 answers

C# - Are Dynamic Parameters Boxed

If I have: void Foo(dynamic X) { } And then: Foo(12); Would 12 get boxed? I can't imagine it would, I'd just like to ask the experts.
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
19
votes
2 answers

Enum Boxing and Equality

Why does this return False public enum Directions { Up, Down, Left, Right } static void Main(string[] args) { bool matches = IsOneOf(Directions.Right, Directions.Left, Directions.Right); Console.WriteLine(matches); …
Greg
  • 23,155
  • 11
  • 57
  • 79
18
votes
3 answers

Is there Boxing/Unboxing when casting a struct into a generic interface?

Possible Duplicate: Structs, Interfaces and Boxing From the MSDN: http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx Boxing is the process of converting a value type to the type object or to any interface type implemented by this value…
paercebal
  • 81,378
  • 38
  • 130
  • 159
18
votes
2 answers

Does the VB.NET "If" operator cause boxing?

Those of us who've worked in VB/VB.NET have seen code similar to this abomination: Dim name As String = IIf(obj Is Nothing, "", obj.Name) I say "abomination" for three simple reasons: IIf is a function, all of whose parameters are evaluated; hence…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
17
votes
2 answers

Why does this cast from short to int fail?

We have some code that archives data from a Microsoft Access database into a MS SQL Server database. Assuming we have a data reader already populated from the Access table and we are adding a parameter to a SqlCommand in preparation for the insert,…
A. Wilson
  • 688
  • 1
  • 6
  • 15
16
votes
5 answers

Object type boxing with a reference type variable

Boxing is when a value type is assigned to an object type. Is it the same when a reference type is assigned to an object? When a type (which isn't object) is assigned, what happens? Is that boxing too? int num=5; object obj = num; …
Mehdi Hadeli
  • 9,784
  • 3
  • 20
  • 20
16
votes
5 answers

How does a generic constraint prevent boxing of a value type with an implicitly implemented interface?

My question is somewhat related to this one: Explicitly implemented interface and generic constraint. My question, however, is how the compiler enables a generic constraint to eliminate the need for boxing a value type that explicitly implements an…
Mike Rosenblum
  • 12,027
  • 6
  • 48
  • 64
16
votes
6 answers

Is boxing involved when calling ToString for integer types?

Very simple question: int a = 5; string str = a.ToString(); Since ToString is a virtual method of System.Object, does it mean that everytime I call this method for integer types, a boxing occurs?
sapito
  • 1,472
  • 3
  • 17
  • 26
15
votes
3 answers

Why auto-boxing marked as a warning?

I understand that auto un-boxing should be done with care because the reference that is being un-boxed can be null. Why is auto-boxing marked as warning as well? Are there some pitfalls I am missing here?
Artium
  • 5,147
  • 8
  • 39
  • 60
1 2
3
39 40