Cameron
Cameron

Reputation: 98746

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

I'm trying to digest this statement in the C# spec, which states (§4.2):

A reference type is a class type, an interface type, an array type, or a delegate type.

I know that structs can implement interfaces. And structs are value types.

So, I'm having trouble reconciling this information. Does this mean that structs behave as reference types when handled via an interface type? That would mean that you could get a reference to a value type...

Upvotes: 2

Views: 144

Answers (5)

supercat
supercat

Reputation: 81143

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>(T thing1, T Thing2) where T:IComparable<T>
{
   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<T> and IComparer<T>).

Upvotes: 1

dthorpe
dthorpe

Reputation: 36072

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.

Upvotes: 3

Richard Schneider
Richard Schneider

Reputation: 35477

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.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283624

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.

Upvotes: 3

Carson63000
Carson63000

Reputation: 4232

Here's a blog post that may be illustrative.

http://blogs.msdn.com/b/abhinaba/archive/2005/10/05/477238.aspx

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.

Upvotes: 3

Related Questions