Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, Data Structure In computer science, a data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently. Learn More, Object-Oriented Programming(OOP) Object-Oriented Programming(OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. Learn More, Programming concepts Almost all programs consist of the same basic 'building blocks', built together in different ways to achieve a particular goal. Variables, data types, sequence, selection, and iteration are some examples of these basic concepts, which all new programmers need to learn. This tag is mainly for all such conceptual Questions. Learn More,

difference between value type and reference type with example in c#

Description:recently I have gone through an interview where I have been asked why a software developer should know about the difference between value type and reference type, by definition I know both terms but what are the condition where I have to choose any one of them and the other couldn't do the same thing for me ?

Posted by: | Posted on: Feb 08, 2019

1 answers

Replies

5

Value Type:

A value type derives from System.ValueType and stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both variables work independently. Value types can be created at compile time and Stored in stack memory, because of this, Garbage collector can't access the stack.
Some Value Types are:
-bool
-byte
-char
-decimal
-double
-enum
-float
-int
-long
-sbyte
-short
-struct
-uint
-ulong
-ushort


Reference Type:

A reference type extends System.Object and used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference Type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection.
Some Reference Types are:
-String
-All arrays, even if their elements are value types
-Class
-Delegates
-Indexers
-Interfaces


Now for further explanation I have created a simple console application which will help you understand the difference in terms of usage.
Code example:

using System;
public class Program
{
public static void Main()
{
MyClass mc=new MyClass();
mc.Number=100;
int number=100;
ChangeValueTypeValue(number);
ChangeReferenceTypeValue(mc);
Console.WriteLine("ValueType="+number);
Console.WriteLine("ReferenceType="+mc.Number);
}
public static void ChangeValueTypeValue(int num ){
num=num+10;
Console.WriteLine("ValueType Inside the method="+ num);
}
public static void ChangeReferenceTypeValue(MyClass myclass){
myclass.Number=myclass.Number+10;
}
}

public class MyClass
{
public int Number;
}

Above code’s output will be as follows:

ValueType Inside the method=110;
ValueType=100;
ReferenceType=110;



Now to get more clarity regarding the above code and its output, what happens here(for value type) is that value type created the copy of the variable with actual data which will work independently, when we have incremented the value inside the method that incremented value has the scope inside the method only. So when we have printed the value inside the method it has been printed as incremented value (110) and when we have printed it outside the method it actually printed the original variable’s value which was 100.
While on the other hand reference type created the copy of the variable which holds the address of another memory location containing the actual data so both copies points to the same data, So when we have incremented the value it actually incremented the value which is shared among both variable (the original one and the copy) and now when we have printed the value for reference type it displayed the incremented value as 110.

Replied by: | Replied on: Feb 11, 2019



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview