views:

204

answers:

7

What is the definition of a value class and reference class in C#?

How does this differ from a value type and reference type?

I ask this question because I read this in the MCTS Self-Paced Training Kit (Exam 70-536). Chapter 1, Lesson 1, Lesson review 4 :

You need to create a simple class or structure that contains only value types. You must create the class or structure so that it runs as efficiently as possible. You must be able to pass the class or structure to a procedure without concern that the procedure will modify it. Which of the following should you create?

A reference class

B reference structure

C value class

D value structure

Correct Answer: D

A Incorrect: You could create a reference class; however, it could be modified when passed to a procedure.

B Incorrect: You cannot create a reference structure.

C Incorrect: You could create a value class; however, structures tend to be more efficient.

D Correct: Value structures are typically the most efficient.

+3  A: 

See the overview on the subject, but seriously follow the msnd links and read the full Common Type system chapter of it. (You could also have asked in a comment in the first, question)

VirtualBlackFox
A: 

Value types store the actual data while reference types store references to the data. Reference types are stored dynamically on the heap while value types are stored on the stack.

Value Types: http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx Reference Types: http://msdn.microsoft.com/en-us/library/490f96s2.aspx

Jeremy T
Not accurate. Value types that are fields of classes are stored on the heap among the rest of the object state.
zvolkov
http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx
Joren
yeah my explanation was a little overly simplistic
Jeremy T
+2  A: 

Value types are passed by value, while reference types are passed by reference.

Edit: value/reference classes
There is no concept of a 'value class' or 'reference class' in C#, so asking for its definition is moot.

Joren
Perhaps if he gave us a link to the document to which he was referring...
Jason Down
I added the document reference
Dran Dane
Reference types are passed by value, but the reference is passed. This is a subtle but important distinction.
Wedge
I see your point. The value-typed variables directly contain data, which is passed by value. Reference-typed variables contain a reference to the data, of which the reference is also passed by value. If you use ref or out, then the variables are passed by reference.
Joren
A: 

When you refer to a value type (that is, by using its name), you're talking about the place in memory where the data is. As such, value types can't be null because there's no way for the memory location to say "I don't represent anything." By default, you pass value types by value (that is, the object you pass in to methods doesn't change as a result of the method's execution).

When you use a reference type object, you're actually using a pointer in disguise. The name refers to a memory location, which then references a place in memory where the object actually lives. Hence you can assign null to a reference type, because they have a way of saying "I point to nowhere." Reference types also allow the object to be changed as a result of methods executing, so you can change myReferenceObject's properties by passing it into a method call.

Lucas Richter
A: 

Reference types are passed to methods by reference and value types by value; in the latter case a method receives a copy of the variable and in the former it receives a reference to the original data. If you change your copy, the original does not change. If you change the original data you have a reference to, the data changes everywhere a reference to the data is changed. If a similar program to your C# program was created in C, generally reference types would be like data using pointers and value types would be normal data on the stack.

Numeric types, char, date, enumerations, and structures are all value types. Strings, arrays, delegates and classes (i.e., most things, really) are reference types.

Mike Graham
A: 

You may be thinking of C++/CLI which, unlike C#, allows the user to declare a "value class" or a "ref class." In C#, any class you declare will implicitly be a reference class - only built-in types, structs, and enums have value semantics. To read about value class in C++/CLI, look here: http://www.ddj.com/cpp/184401955

Value classes have very little functionality compared to ref classes, and are useful for "plain old data"; that is, data which has no identity. Since you're copying the data when you assign one to another, the system provides you with a default (and mandatory) copy constructor which simply copies the data over to the other object.

To convert a value class into a reference class (thereby putting it on the garbage-collected heap) you can "box" it.

To decide whether a class you are writing is one or the other, ask yourself whether it has an identity. That usually means that it has some state, or has an identifier or a name, or a notion of its own context (for example a node pointing to nearby nodes).

If it doesn't, it's probably a value class.

In C#, however, value classes are declared as "structs".

Drew Hoskins
A: 

If my understanding is correct, you can accomplish a "value class", or immutable class, through the use of readonly member variables initialized through the constructor. Once created, these cannot be changed.

Ryan Emerle