Recently I got a chance to talk to a bunch of entry level professionals working on a software development project. During the discussion, a question came up on what is value type, what is a reference type and what is the difference between the two. Answer I got for those questions weren't satisfactory.
What is a value type?
When you pass a variable to a method, if the variable value gets passed instead of the variable itself, then they are called value types. Didn't understand?. Don't worry, let's consider an example.
{
int salary = 1000;
IncrementSalary(salary, 100);
Console.Writeline(salary);
}
public statis IncrementSalary(int salary, int byAmount)
{
salary = salary + byAmount;
}
What is the output of the above C#.NET example?
- Is it 1000?
- Is it 1100?
Actual output
1000
If your answer was 1100, then your understanding of value types is incorrect. Read further to understand, "What is a value type"?
When you call a method "IncrementSalary" and the pass the variable "salary" to it, actually an another variable, which is
- pointing to a different memory location than that of salary variable
- AND
- having the same value of the salary variable
is passed.
So whatever variable value gets updated inside the "IncrementSalary" method, the value stored in the new memory location will get updated. Hence updates to the variable will not be reflected in the caller method's variable (i.e., Main() method in this case)
Examples of value types in .NET C#
More or less all structures, i.e, int, decimal, DateTime, long etc
What is reference type?
When you pass a variable to a method, if the variable reference gets passed to the passed to the method parameter then they are called reference types. Let's consider a sample C# .NET tutorial example.
public int Discount {get;set;}
}
public static void Main(string[] args
{
PromotionOffer offer = new PromotionOffer();
offer.Discount = 10;
IncrementOfferDiscount(offer, 10);
Console.Writeline(offer.Discount);
}
public static IncrementOfferDiscount(PromotionOffer offer,
int byPercenatge)
{
offer.Discount += byPercentage;
}
What is the output of the above C#.NET example?
- Is it 10?
- Is it 20?
Actual output
20
If your answer was 10, then your understanding on reference type is incorrect. Read further to understand, "what is a reference type".
When you call the method "IncrementOfferDiscount" and pass in the promotionOffer variable, the method gets the same reference of the variable used by the calling method(in this case, it is Main() method). So whenever you update the properties of the reference type variable in the method called, it will update the same copy of the values held by the calling method.
So after you call the IncrementOfferDiscount() method and print the Discount of promotionOffer object, you will see the updated value 20.
Examples of reference types in .NET C#
All inbuilt classes in the .NET framework and custom classes you define are reference types.
Now I hope you understood what is value type, what is reference type and the difference between value type and reference type.
Note:
- The built-in String class is a reference type, but .NET framework treats it differently, because of string intern concept(which is same in JAVA as well)
- The value types can be passed to a method as reference types using out and ref keywords. Learn more about ref and out keywords in C# .NET by reading my another post "ref v/s out in .NET"
No comments:
Post a Comment