boxing

C# compiler + generic code with boxing + constraints

Let's examine the MSIL code generated for the following generic method: public static U BoxValue<T, U>(T value) where T : struct, U where U : class { return value; } Look: .method public hidebysig static !!U BoxValue<valuetype .ctor ([mscorlib]System.ValueType, !!U) T,class U>(!!T 'value') cil managed { .maxstack 8 IL_00...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let's assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying String.class would return an empty String, supplying an Integer.class would return an Integer whose initial value is zero, and so on. But ho...

Using the conditional operator in conjunction with IsAjaxRequest to return ActionResult

Since there is no implicit conversion between Mvc.JsonResult and Mvc.ViewResult I cannot just utilize a conditional operator but instead end up with a cast. Which leads me to my question is the performance hit I will take for boxing the JsonResult worth it or should I just do a normal if...else block? The code below appears inside a no...

How to prevent unboxing - boxing memory overhead when reading arbitrary sql rows

Guys, I'm writing a class to represent a row from a SQL query. I want the field data to be accessed via the indexer property of the class. This is straightforward enough if I load the data into an internal List of Object. I've already tried this and am not happy with the boxing for the primitives. Boxing increases the memory requirement...

Variable number of arguments without boxing the value-types?

public void DoSomething(params object[] args) { // ... } The problem with the above signature is that every value-type that will be passed to that method will be boxed implicitly, and this is serious performance issue for me. Is there a way to declear a method that accepts variable number of arguments without boxing the value-type...

Internal compiler error ArrayIndexOutOfBoundsException: -1 ... generateUnboxingConversion

I got some weird exception when trying to compile this: Byte b = 2; if (b < new Integer(5)) { ... } Is it a valid check (unboxing-implicit cast - unboxing)? ...

Boxing and unboxing: when does it come up?

So I understand what boxing and unboxing is. When's it come up in real-world code, or in what examples is it an issue? I can't imagine doing something like this example: int i = 123; object o = i; // Boxing int j = (int)o; // Unboxing ...but that's almost certainly extremely oversimplified and I might have even done boxing/...

Java automatic unboxing - is there a compiler warning?

I am a big fan of auto-boxing in Java as it saves a lot of ugly boiler plate code. However I have found auto-unboxing to be confusing in some circumstances where the Number object may be null. Is there any way to detect where auto-unboxing is occurring in a codebase with a javac warning? Any other solution to detect occurrences of unboxi...

Avoid boxing in DbParameter.Value?

Hi I'm using ADO.NET to communicate some db, and searching for a way to avoid boxing when setting the DbParameter.Value property to value-type. Is there a way to avoid boxing in DbParameter.Value? Thanks. ...

Why do we need boxing and unboxing in C#?

Though I know what boxing and unboxing is. But I can't comprehend the real use of it. Why and where should I use it. short s=25; object objshort=s; //Boxing short anothershort=(short)objshort; //UnBoxing Please help. Thanks in advance. ...

Store int32_t in an NSDictionary

Hi, How can I store a variable of type int32_t (e.g. for ABPropertyID) in an NSDictionary? [NSNumber numberWithInt:...] doesn't seem to work. Thanks From the comments: NSLog(@" %@ %@ ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty); Prints: 0 (null) Any ideas? ...

Are classes boxed? .NET

I was wondering, does a class get boxed? I always assumed every class had a virtual table which can be used to identify the class, so does it need to be boxed? ...

Does boxing create garbage in .NET?

Hi! I'm wondering whether boxing a value type in an object is a special case or whether the "box" constructed by .NET becomes garbage (that the GC has to collect) after any references to it are dropped. For example, StringBuilder.AppendFormat() has these overloads: StringBuilder.AppendFormat(string format, object arg0); StringBuilder....

Java: Are there some quasi-standard APIs out there, which do int[] <-> Integer[] and similar?

Hello, everyone! I need to do lots of conversions between primitivetype[] and boxedtype[] (both directions). Such as: Integer[] <-> int[], Double[] <-> double[], ... I wanted to know, if there's some quasi-standards APIs out there, which provide such functionality, before I write such utility methods by myself. Java has 8 primitive ty...

What Performs Worse: Reflection or Boxing?

I'm working on creating my own DI framework that creates delegate factories as a learning exercise. My way of building typed delegates is to use expressions to create a function that calls a static method with reference to my container and any constructor params. This thrown up an interesting question with regards to value types. Whic...

Is there any difference between using .GetValueOrDefault(0) and If(variable, 0) with nullable types?

Is there any difference between the 2 methods below for calculating c ... specifically boxing/unboxing issues? Dim a As Integer? = 10 Dim b As Integer? = Nothing Dim c As Integer ' Method 1 c = If(a, 0) + If(b, 0) ' Method 2 c = a.GetValueOrDefault(0) + b.GetValueOrDefault(0) ...

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 if obj is nothing in the above call then a NullReferenceException will be thrown. This...

Is there a performance disadvantage for WPF''s Control properties?

I am learning WPF and it seems like some properties like Content are of type Object. When you have a very complex UI with 1000s of controls, would this be a problem with boxing, etc? I see that this provides a lot of power (having Content to take anything), but just thought I should ask people's opinions about this. ...

Is there a native Java method to box an array?

Is there a implemented Java method in jdk to do this? public static Byte[] box(byte[] byteArray) { Byte[] box = new Byte[byteArray.length]; for (int i = 0; i < box.length; i++) { box[i] = byteArray[i]; } return box; } ...

How do I avoid boxing/unboxing when extending System.Object?

I'm working on an extension method that's only applicable to reference types. I think, however, it's currently boxing and unboxing the the value. How can I avoid this? namespace System { public static class SystemExtensions { public static TResult GetOrDefaultIfNull<T, TResult>(this T obj, Func<T, TResult> getValue, TR...