top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are data types in C#? What is the difference between value type and reference types?

0 votes
598 views
What are data types in C#? What is the difference between value type and reference types?
posted Mar 22, 2017 by Pooja Bhanout

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Value types are stored in the program execution stack and directly contain their value. Value types are the primitive numeric types, the character type and the Boolean type: sbyte, byte, short, ushort, int, long, ulong, float, double, decimal, char, bool. The memory allocated for them is released when the program exits their range, i.e. when the block of code in which they are defined completes its execution.

Reference types keep a reference (address), in the program execution stack, and that reference points to the dynamic memory (heap), where their value is stored. The reference is a pointer (address of the memory cell) indicating the actual location of the value in the heap.

answer Mar 22, 2017 by Shweta Singh
0 votes

Value Type
Value type variables can be assigned a value directly. They are derived from the class System.ValueType.

The value types directly contain data. Some examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.

reference types
The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.

In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.

answer Mar 29, 2017 by Aanchal Rastogi
...