What is a value class and what is a reference class in C #?

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

How is this different from value type and reference type ?

I ask this question because I read it in the MCTS self-study kit (exam 70-536). Chapter 1, Lesson 1, Lesson 4 Overview:

You need to create a simple class or structure containing only value types. You must create a class or structure so that it runs as much as possible. You must be able to pass the class or structure of the procedure, without the procedure changing it. Which one should I create?

Control class

B reference structure

Value Class C

D value structure

Correct answer: D

A False: you can create a reference class; however, this can be changed during the transfer procedure.

B False: you cannot create a reference structure

C Wrong: you can create a class value; however, structures are generally more efficient.

D Correct: Value structures are usually the most efficient.

+6
c # oop terminology
source share
7 answers

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

Edit: values ​​/ reference classes
In C #, there is no concept of a value class or reference class, so the request for its definition is controversial.

+2
source share

See a review on this, but seriously follow the msnd links and read the full "General Type System" . (You might also ask in the comments in the first question)

+3
source share

Perhaps you are thinking of C ++ / CLI, which, unlike C #, allows the user to declare a "value class" or "ref class". In C #, any class that you declare will be an implicit reference class - only built-in types, structures, and enumerations have semantics of values. To read the 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 that does not have an identity. Since you copy data when assigned to each other, the system provides you with a default copy constructor (and a must) that simply copies the data to another object.

To convert a value class to a reference class (thereby putting it in a heap of garbage), you can "paste" it.

To decide whether the class you are writing is one or the other, ask yourself if it has a personality. This usually means that it has a certain state or has an identifier or name, or the concept of its own context (for example, a node pointing to neighboring nodes).

If it is not, then this is probably a class of values.

However, in C # value classes are declared as "structures".

+2
source share

Value types store actual data, and reference types store data references. Link types are stored dynamically on the heap, and value types are stored on the stack.

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

+1
source share

When you refer to a value type (that is, using its name), you are talking about the place in memory where the data is located. Thus, the types of values ​​cannot be null, because there is no way for memory to say "I cannot imagine anything." By default, you pass value types by value (that is, the object you pass to methods does not change as a result of the method).

When you use an object of a reference type, you are actually using a hidden pointer. The name refers to the memory cell, which then refers to the place in memory where the object actually lives. Therefore, you can assign null to a reference type because they have a way of saying "I am pointing nowhere." Link types also allow you to modify an object as a result of the execution of methods, so you can change the properties of myReferenceObject by passing it to a method call.

+1
source share

Link types are passed to methods by reference and value types by value; in the latter case, the method receives a copy of the variable, and in the first it receives a link to the source data. If you change your copy, the original will not change. If you change the source data that you are referring to, the data changes wherever the link to the data changes. If a similar program was created in C for your C # program, usually reference types will look like data with pointers, and value types will 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.

0
source share

If my understanding is correct, you can execute a "value class" or an immutable class using readonly member variables initialized via the constructor. Once created, they cannot be changed.

0
source share

All Articles