Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Value Class and what is a reference Class in C#?

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.

like image 435
Bastien Vandamme Avatar asked Nov 02 '09 01:11

Bastien Vandamme


People also ask

What is a value class?

What are Value Classes. At a fundamental level: value classes define objects which, once created, never change their value. A variable of a value type may only be changed by re-assigning to that variable.

What is the difference between value and reference types?

There are two kinds of types in Visual Basic: reference types and value types. Variables of reference types store references to their data (objects), while variables of value types directly contain their data.

What is difference between C type and value type reference?

A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are stored in the stack.

What is value and reference type in C#?

Value types and reference types are the two main categories of C# types. A variable of a value type contains an instance of the type. This differs from a variable of a reference type, which contains a reference to an instance of the type.


2 Answers

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

like image 185
Drew Hoskins Avatar answered Oct 12 '22 15:10

Drew Hoskins


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)

like image 26
Julien Roncaglia Avatar answered Oct 12 '22 15:10

Julien Roncaglia