Two uses of using keyword in C#
Most of the C# interviewers will attempt to ask this question:
What are the uses of using keyword?
There are two uses of using keyword in C#
1) Using as Directive in C# can be used for importing namespaces to your classes. Everybody knows this.
2)Another most useful advantage is it can be used in codeblocks what is does is it calls the Dispose() method automatically to release the resources and also automatically implements the try catch blocks for you.Here it is called a using statement.So the advantage is that we dont have to explicitly write a try-catch block and our code looks small.
C# Function Parameter Passing by Reference in Detail
Here are is a simple test to play with the function parameter passing in C#.
As you guys know int, float, char, enum and struct are value types and class is a reference type.
In case of C#, lot people think objects are passed by reference to a function, but the reality is object references are passed by value.
How to prove it? Here is the code. Analysis of the code is below so keep reading….
using System;
namespace APP1
{
class Program
{
static void Main(string[] args)
{
Program Test = new Program();
Int first = new Int();
first.i = 6;
Int second = first;
Test.change(ref second);
Console.WriteLine("first: " + first.i);
Console.WriteLine("second: " + second.i);
CInt cfirst = new CInt();
cfirst.i = 6;
CInt csecond = cfirst;
Test.change(ref csecond);
Console.WriteLine("cfirst: " + cfirst.i);
Console.WriteLine("csecond: " + csecond.i);
Console.Read();
}
void change(ref Int param)
{
param= new Int();
}
void change(ref CInt param)
{
param = new CInt();
}
}
public class CInt
{
public int i;
}
public struct Int
{
public int i;
}
}
Here I have a struct Int and a class CInt.
First lets look at the Int struct object and the Change function:
On a normal Change function with out “ref” keyword the console would print 6 for both first and second object.
Here we have a ref and we are trying to initialize the parameter object.
void change(ref Int param)
{
param= new Int();
}
In the Main we are calling
Test.change(ref second);
So the output we get is
first: 6
second: 0
This is the behaviour of struct type you can say it is passed by value.
Now lets come to the Class type:
void change(ref CInt param)
{
param = new CInt();
}
We have the default values set to 6 before the call:
Test.change(ref csecond);
Whet do you think the output will be?
You may think it would have reset both cfirst and csecond to 0, but the reality is
you will get the output as
cfirst: 6
csecond: 0
This is because of the reason the object references are passed by value.
However, if you change any of the class members value
for example if you set on the change function with CInt as parameter with or without “ref” keyword and without initializing the param object.
param.i = 7;
you will notice it will affect the csecond and cfirst (and the output will be 7) which will give you an impression it is infact objects are passed by references.
The only way to track the behaviour of object references are passed by value is by trying to initializing the parameter inside the function.
I hope this helps you understand the parameter passing in C#.
If you have any questions or comments please reply here.
leave a comment