Slight confusion over reference types and value types in the C# spec?

Here's a blog post that may be illustrative blogs.msdn.com/b/abhinaba/archive/2005/1... Yes, structs that implement interfaces get boxed as reference types if you handle them as interfaces, and yes, this can cause problems if you're not careful.

Here's a blog post that may be illustrative. blogs.msdn.com/b/abhinaba/archive/2005/1... Yes, structs that implement interfaces get boxed as reference types if you handle them as interfaces, and yes, this can cause problems if you're not careful.

2 Ah, boxing, of course! Suddenly my puzzled mind isn't so puzzled anymore... thanks! – Cameron Jun 10 at 4:05.

That's correct. When a value type is used in context where an interface reference is required, it gets boxed. Same thing happens if System.

Object is required. What you can't have is an interface reference to a value type instance on the stack, or inside another type. The boxing process creates a copy.

Yes, you can get a reference to a value type. Any time a value type is assigned to a variable or passed as a parameter to a method that expects an Object type, the value type is implicitly wrapped in an object instance - a process called boxing. Boxing is creating an object reference that contains a value.

When the boxed object is assigned to or used like a value type, then it is unboxed and the value is extracted.

Yes, structs can implement an interface BUT they are not an interface type. A struct is a value type and when required will be boxed.

A struct which implements an interface will be boxed if it is cast to the interface, but not if it is cast to a generic type which is constrained to implement the interface. For example: void Compare(T thing1, T Thing2) where T:IComparable { return thing1. CompareTo(Thing2); } Note that while the above code avoids boxing when using structs, comparing two objects of value-type T will require three copy operations.

If the parameters were passed by reference instead of by value, performance with value types would be enhanced, at the expense of impaired reference-type performance (and, of course, compatibility with the existing IComparable and IComparer).

Interesting. Thanks for your answer! – Cameron Oct 6 at 17:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions