unboxing

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...

Unboxing to unknown type

I'm trying to figure out syntax that supports unboxing an integral type (short/int/long) to its intrinsic type, when the type itself is unknown. Here is a completely contrived example that demonstrates the concept: // Just a simple container that returns values as objects struct DataStruct { public short ShortVale; public int In...

Why does autoboxing in Java allow me to have 3 possible values for a boolean?

Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html "If your program tries to autounbox null, it will throw a NullPointerException." javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess. but let'...

C# Type Conversion

Hi guys, I have two objects. Object A and Object B. Object A is an instance of a class that was generated from several XSD files. Used xsd.exe /c and compiled them. Now I have my new object. I also have a web service, returning something very similar to object A. So right now I have something along the lines of this: WebService.fo...

Unboxing an untyped field in a DataRow Without a Casting Error

The data layer of my ASP.Net app calls a stored proc to get a small (one record) amount of information about a visitor upon login. I pass in their phone number and the sproc, using a simple SELECT, passes back 5 fields, the first of which is the primary key, a BIGINT. My data layer gets the DataRow and tries to create a data object wit...

Does unboxing just return a pointer to the value within the boxed object on the heap?

I this MSDN Magazine article, the author states (emphasis mine): Note that boxing always creates a new object and copies the unboxed value's bits to the object. On the other hand, unboxing simply returns a pointer to the data within a boxed object: no memory copy occurs. However, it is commonly the case that your code wil...

Question about boxing and unboxing.

I got the following code: object var3 = 3; Console.WriteLine(var3.GetType().ToString()); Console.WriteLine(typeof(object).ToString()); The output is: System.Int32 System.Object Why aren't they both System.Object? Thanks in advance. ...

Generic method, unboxing nullable enum

I've made the following extension method ... public static class ObjectExtensions { public static T As<T>(this object pObject, T pDefaultValue) { if (pObject == null || pObject == DBNull.Value) return pDefaultValue; return (T) pObject; } } ... which i use for e.g. reading data like so: string f...

Of which things should I take care if I'm using unboxed type (like Int#) in Haskell / GHC?

I'm trying to write a small script which parses and executes Brainfuck code, to understand the GHC options of optimization, I'm trying to optimize the code in order to be a bit faster and to understand what's going on there. On of the parts is the internal represantation of BF-code, I use a special datatype for this. Here's the sourceco...

Assigning an array of structure to another array of same structure

In Vb.net I am trying to assign an array of structure to another array of same structure Dim info() As assemblyInfo Dim info2() As assemblyInfo Structure assemblyInfo Dim Name As String Dim ClassTpys() As ClassTyp End Structure Private Sub test() info2 = info any change in ifo gets reflected in info2, th...

Boxing and UnBoxing on Android

Hello. I'm developing an Android application. I have the following interface: public interface IDBAdapter { public enum Table { ... } public int insertItem(Table table, Object item); public boolean removeItem(Table table, long rowIndex); public Cursor getAllItemsCursor(Table table); public Cursor setCu...

Get value from Dictionary<string, object> without unboxing?

Hey, I was wondering if it's possible to run the following code but without the unboxing line:- t.Value = (T)x; Or maybe if there is another way to do this kind of operation? Here is the full code:- public class ValueWrapper<T> { public T Value { get; set; } public bool HasValue { get; set; } public ValueWrapper() ...

Why does unboxing enums yield odd results?

Consider the following:: Object box = 5; int @int = (int)box; // int = 5 int? nullableInt = box as int?; // nullableInt = 5; StringComparison @enum = (StringComparison)box; // enum = OrdinalIgnoreCase StringComparison? nullableEnum = box as StringComparison?; // nullableEnum = null. 2 things:: Why can I unbox to StringComparison? I...