boxing

What is boxing and unboxing and what are the trade offs?

I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. ...

Do generic interfaces in C# prevent boxing? (.NET vs Mono performance)

I have a C# interface with certain method parameters declared as object types. However, the actual type passed around can differ depending on the class implementing the interface: public interface IMyInterface { void MyMethod(object arg); } public class MyClass1 : IMyInterface { public void MyMethod(object arg) { My...

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

I was just participating in this question: http://stackoverflow.com/questions/436211/is-everything-in-c-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 this article which doesn't exactly specify the use case we're describing. I...

Boxing, what's your preference and which do you think is faster?

In short, I think boxing is an annoyance. Let's look at some alternatives... public class Box<T> where T : struct { public T Value { get; set; } public static implicit operator T(Box<T> box) { return box.Value; } } System.Int32 derives from abstract class System.ValueType which derives from class System.O...

Instantiate "AS" keyword

I've recently started working with JSON and the ExtJs framework and I've come across the following code in an example. we retrieve the information from the frontend using this: object updatedConfig = JavaScriptConvert.DeserializeObject(Request["dataForm"]); Then in the example they do the following: JavaScriptObject jsObj = update...

Questions about boxing

I know that boxing is a popular concept with plenty of information available on it, but I have a few questions which I can't really find answers to: 1) If boxing leads to a value type (struct) being converted to an object (Reference type), or reference type, then why use a value type which will be boxed and incur a performance penalty? ...

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); Console.Read(); } public static bool IsOneOf(Enum self, params Enum[] values)...

Boxing vs Unboxing

Hello, Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast to a reference type, we call it boxing and vice versa. Then he asked me to calculate this: int i = 20; object j = i; j = 50; What is i? I messed it ...

Use cases for boxing a value type in C#?

There are cases when an instance of a value type needs to be treated as an instance of a reference type. For situations like this, a value type instance can be converted into a reference type instance through a process called boxing. When a value type instance is boxed, storage is allocated on the heap and the instan...

Why do some languages need Boxing and Unboxing ?

Hi, This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector<double> dummy; I have some experience with Java, but I was really surprised because I had to write som...

How to convert int[] into List<Integer> in Java?

How do I convert int[] into List<Integer> in Java? Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java. ...

What is the difference between boxing/unboxing and type casting?

What is the difference between boxing/unboxing and type casting? Often, the terms seem to be used interchangeably. ...

C# non-boxing conversion of generic enum to int?

Given a generic parameter TEnum which always will be an enum type, is there any way to cast from TEnum to int without boxing/unboxing? See this example code. This will box/unbox the value unnecessarily. private int Foo<TEnum>(TEnum value) where TEnum : struct // C# does not allow enum constraint { return (int) (ValueType) val...

When does a using-statement box its argument, when it's a struct?

I have some questions about the following code: using System; namespace ConsoleApplication2 { public struct Disposable : IDisposable { public void Dispose() { } } class Program { static void Main(string[] args) { using (Test()) { } } static Disposable Test() ...

Type Casting an Object using a "Type" Object in C#

This one has proven to be a little tricky for me so far. I am wondering if it is possible to type cast an object using a System.Type object. I have illustrated below what I mean: public interface IDataAdapter { object Transform(object input); Type GetOutputType(); } public class SomeRandomAdapter : IDataAdapter { public ob...

Hidden Boxing in the BCL?

Recently I became aware that there are some parts in the BCL that still use some "legacy" code that was probably written before generics were introduced in v2.0 of the framework. Apparently, parts of that "legacy" code may cause the CLR to perform numerous boxing/unboxing operations. Since excessive usage of boxing is never a good thing...

How to determine if type needs to be boxed?

MSDN docs say that only value types need boxing, but this does not apply to string, which is a value type and does not need to be boxed. I initially tried Type.IsValueType, but since that returns true for string, I can't use it to determine whether a type really needs to be boxed. Are there any other methods you are aware of? Is string t...

Why does generic method with constaint of T: class result in boxing?

Anyone any idea why a generic method which constrains T to class would have boxing instructions in the generates MSIL code? I was quite surprised by this since surely since T is being constrained to a reference type the generated code should not need to perform any boxing. Here is the c# code: protected void SetRefProperty<T>(ref T pr...

C++/CLI: Boxing and Generic Lists

I am trying to create a generic list of references to PointF objects. (No, I am not looking to create a generic list of PointF objects.) However, the following line fails to compile: Generic::List<PointF^> ^pointList; // Generates error C3225 On the other hand, creating an array of PointF references works without a problem as follows:...

Avoiding unnecessary boxing in DLR

I'm playing with DLR to get a better understanding of it. I'm not completely familiar yet with all its concepts and its terminology so sorry for any terminological or conceptual mistakes in my question. Basically, the way I understand it is that you pass around objects in expression trees but you use binders in order to expose your obj...