8

I've faced a lot of confusion with regards to differences between generic reference types vs. generic value types whilst writing my API.

What are the differences with regards to constraints and functionalities (most important/easily overlooked) between the two?

class ReferenceGeneric <T> where ???
{

}

and

struct ValueGeneric <T>: where ???
{

}

Edit #1 Just to clarify the question and what I am after: I want to know what you can do with Generic reference types AND cannot do with generic value types (and vice versa)

Edit #2 Further clarifications: How can T be constrained if generic type is reference or value type? Are there differences as to how each type can be constrained?

6
  • You question is very confusing... there is an answer out there but what are you after? Commented Mar 1, 2009 at 21:13
  • you said you have to tweak sometimes. can you give an example? Commented Mar 1, 2009 at 21:15
  • I've elaborated on the question in the edit. Commented Mar 1, 2009 at 21:18
  • your question would be more interesting if you phrased it as: what can you do differently in A and B. A) class Foo<T> where T: struct { } B) class Foo<T> where T : class { } Commented Mar 1, 2009 at 22:04
  • Perhaps, but I was interested in something else -- as stated above :) Commented Mar 2, 2009 at 5:06

2 Answers 2

15

Be aware that anything declared as a struct is always a value type, and anything declared as a class is always a reference type. In other words, List<int> is still a reference type, and if you had:

struct Foo<T>
{
    T value;
}

then Foo<string> would still be a value type.

As for what you can do with the generic types - they really just follow the normal rules for value types and reference types; as for what you can do with an value of type T within the type, that depends on whether/how T is constrained. It doesn't vary based on whether the generic type itself is a struct or a class though.

EDIT: Sasha mentions Nullable<T> in the comments. I'm not sure what "exception" is meant here - other than Nullable<T> doesn't satisfy either the "where T : struct" or "where T : class" constraint. It's still a value type though (which is part of the point).

Sign up to request clarification or add additional context in comments.

3 Comments

Jon, there is an exception if the struct type is Nullable. Would you elaborate on that as well? thx
" anything declared as a class is always a value type" typo?
Jon: Fixed your typo to avoid confusion :)
11

In response to Edit2: You can limit the types allowed to reference or value by the following:

Reference:

class ReferenceGeneric <T> where T: class
{

}

Value:

struct ValueGeneric <T> where T: struct 
{


}

From the following page on MSDN http://msdn.microsoft.com/en-us/library/d5x73970.aspx

1 Comment

Can you constrain struct by class: struct ValueGeneric <T> where T: class { }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.