boxing

How does the CLR know the type of a boxed object?

When a value type is boxed, it is placed inside an untyped reference object. So what causes the invalid cast exception here? long l = 1; object obj = (object)l; double d = (double)obj; ...

.NET: Strange behaviour of double.Equals() when boxing.

What's going on here? int zero = 0; double x = 0; object y = x; Console.WriteLine(x.Equals(zero)); // True Console.WriteLine(y.Equals(zero)); // False ...

Fundamental question about boxing / c#

Is it possible to change the value stored inside bar after it has been added? I have tried 'boxing' the string foo but it doesnt work. string foo = "aaaaaaa"; var bar = new System.Web.UI.HtmlControls.HtmlGenericControl("div") { InnerHtml =foo }; foo = "zzzzzz"; plcBody.Controls.Add(bar);//want this to contain 'zzzzzz' ...

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

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

Boxing & Unboxing

var i = new int[2]; Is "i" considered to be boxed? ...

Is converting this ArrayList to a Generic List efficient?

The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will always contain one or more objects of type Grid_Heading_Blk. I've considered changing this ArrayList to a generic List, but I'm unsure if the conversion operation will be so expensive as to nullify the benefits of working with the generic list. Curren...

Structs, Interfaces and Boxing

Take this code: interface ISomeInterface { public int SomeProperty { get; } } struct SomeStruct : ISomeInterface { int someValue; public int SomeProperty { get { return someValue; } } public SomeStruct(int value) { someValue = value; } } and then I do this somewhere: ISomeInterface someVariable = ne...

Is casting to an interface a boxing conversion?

I have an interface IEntity public interface IEntity{ bool Validate(); } And I have a class Employee which implements this interface public class Employee : IEntity{ public bool Validate(){ return true; } } Now if I have the following code Employee emp1 = new Employee(); IEntity ent1 = (IEntity)emp1; // Is this a boxing co...

Why null == 0 throws NullPointerException in Java?

It was very confusing to me to observe this situation: Integer i = null; String str = null; if (i == null) { //Nothing happens ... } if (str == null) { //Nothing happens } if (i == 0) { //NullPointerException ... } if (str == "0") { //Nothing happens ... } So, as I think boxing operation is executed fi...

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

Should generic constraints be preferred to using interfaces as parameter types?

Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0; } Now, if I pass an int to this method, it gets boxed. Wouldn't it therefore be better to define the above method as follows? public static bool IsPositive<T>(T value) where T : IComparable<int> { return valu...

Unboxing uint/int without knowing what's inside the box

I have an object o that is known to be a boxed int or uint: object o = int.MinValue object o = (uint)int.MinValue // same bytes as above I don't know what's in the box, all I care about is that there's 4 bytes in there that I want to coerce to an int or uint. This works fine in an unchecked context when I have values (instead of boxes...

Assigning integer value to a Float wrapper in Java

The following works float a=3; but the following doesn't: Float a=3; Shouldn't 3 be automatically promoted to float (as widening conversions don't require an explicit cast) and then Boxed to Float type ? Is it because of a rule I read in Khalid Mogul's Java book ? Widening conversions can't be followed by any boxing convers...

What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T>

What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T> For me they look similar, but they are not! source is of type IEnumerable<T>, but it is boxed as an object. Edit Here is some Code: public class PagedList<T> : List<T>, IPagedList { public PagedList(object source, int index, int pageSiz...

Is a C# struct ever boxed when used as the return value of a function?

A simple question, but I haven't found a definitive answer on Stack Overflow. struct foo { int x; int y; int z; } foo Func() { return new foo(); } void Func2() { foo f = Func(); // did boxing and unboxing occur? } Is a C# struct (value type) always copied to the stack when returned fro...

How is the boxing/unboxing behavior of Nullable<T> possible?

Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable<T> can be assigned to null. For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T>: public static implicit operator Nullable...

Should I use struct or class?

I am in a classic design dilemma. I am writing a C# data structure for containing a value and measurement unit tuple (e.g. 7.0 millimeters) and I am wondering if I should use a reference type or a value type. The benefits of a struct should be less heap action giving me better performance in expressions and less stress on the garbage co...

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

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