A Commonly Asked C# .Net Interview Question!
So…. let’s talk about interviews, specifically programming interviews, even less abstract a particular interview question. What are reference and value types? If you’re not familiar with the terms, just know as a C# .Net developer you’ve used them. Strings, arrays and objects of classes are reference types the rest are value types. I hope that makes things easier, but let me dive in deeper. These types’ help our programs run efficiently and save memory. As a developer we want to avoid memory leaks and the .Net framework gives us the advantage to do so, by using garbage collecting. This means memory is constantly being freed up for consumption.
There are two sections for storing data, the stack and the heap. Visualize a stack and a heap of objects and you will be able to conceptualize how data is being stored. The stack keeps track of the current state of a program. It contains a method’s local variables (including parameters) and places them in a frame. Frames are stacked one on top of another and when methods are used the frame is discarded. The heap is not as organized with its main concern being data access. Also, unlike the stack data in the heap isn’t discarded until the .Net Framework manages the memory. Garbage collecting changes the location of data in memory and that’s why C# implements reference types and not just pointers. Remember, value types have values and reference types are only references. Value variables and the values assigned to them are stored on the stack while reference variables are stored on the stack, but the value reference is stored on the heap. The reference variable refers to a location in memory on the heap. Reference Types use reference semantics and value types use copy semantics. All of the information above affects the behavior of the types when used. Try out the examples below to get a better understanding. Hope to hear your results!
//Example 1
int a = 4;
int b = 8;
b++;
Console.WriteLine(a);
b = a;
b = 6;
//Notice each variable keeps its original values
Console.WriteLine(a);//a=4
Console.WriteLine(b);//b=6
//Example2
int[] a = new int[] { 1, 2, 3, };
int[] b = a;
Console.WriteLine(a[0]);
b[0] = 20;
//Notice changing variable b[0] also changes variable a[0]
Console.WriteLine(a[0]);
Console.WriteLine(b[0] );
//Example3
int number1 = 2;
int number2 = 4;
Console.WriteLine(number1);
Console.WriteLine(number2);
//Notice variable b did not change variable a
//Notice method effects results
Numbers(number1, number2);
//notice now variable number1 and number2 are now their original
Console.WriteLine(number1);
Console.WriteLine(number2);
}
static void Numbers(int a, int b)
{
a = a + a;
b = 20;
Console.WriteLine(a);
Console.WriteLine(b);
}
}
Example4
Student x = new Student();
Student y = new Student();
x.age = 6;
y.age = 12;
//variable x and y are equal to the original values
Console.WriteLine(x.age);
Console.WriteLine(y.age);
//method effects values of variables
//notices changing b.age does not effect a.age
Numbers(x, y);
//notice numbers reflect the change made in the method
Console.WriteLine(x.age);
Console.WriteLine(y.age);
}
class Student
{
public int age;
}
static void Numbers(Student a, Student b)
{
a.age = a.age * a.age;
b.age = a.age;
b.age = 8;
Console.WriteLine(a.age);
Console.WriteLine(b.age);
}
Hm, I think I understand what you mean, but I may need a bit more explanation on the examples. Where would I put them to see the results. I got a bit confused on the a = A + a as well as the b++ part. Does that mean b plus itself or b plus 1 more? Thanks!