.NET Diary

October 9, 2013

Different types of Parameters in C#

Different types of Parameters in C#: Parameters are means of passing values to a method.
There are four different ways of passing parameters to a method in C#.
1. Value default parameter type in C#.
actual value is passed to the function, which means changes made to the parameter is local to the function and is not passed back to the calling part.
2. Out “out” parameters are output only parameters meaning they can only pass back a value from a function.
3. Ref “ref” parameters are input/output parameters meaning they can be used for passing a value to a function as well as to get back a value from a function.
4. Params “params” parameters is a very useful feature in C#. “params” parameter are used when through a variable number of arguments need to be passed. The “params” should be a single dimensional or a jagged array.
Ref vs. Out //Difference: Ref vs. Out
public void funcRefOut()
int a = 0, b, c = 0, d = 0;//Assigning b = 0 will not make any effect, it should be assigned a value in calling funcd = ParamTest(a, out b, ref c);
richTextBox1.Text = richTextBox1.Text + “\n” + “a = {0} ” + a;
richTextBox1.Text = richTextBox1.Text + “\n” + “b = {0} ” + b;
richTextBox1.Text = richTextBox1.Text + “\n” + “c = {0} ” + c;
richTextBox1.Text = richTextBox1.Text + “\n” + “d = {0} ” + d;
}public static int ParamTest(int a, out int b, ref int c)
a = 10;
b = 20;//Value assign must
c = 30;//Value assign not compulsury
//b = 20;//Error 1 The out parameter ‘b’ must be assigned to before control leaves the current method
//c = 30;// ref type may not be assigned
return 40;
C# Value
Ref Type
.NET uses two different areas of Ram for holding variables;. the stack & heap.
fixed in size when the application is linked Stack [LIFO – bucket]
holds value type variables plus return addresses for functions. All numeric types, ints, floats and doubles along with enums, chars, bools and structs are value types.
basically all unallocated memory. Heap
holds variables created dynamically- known as reference variables and mainly instances of classes(objects) or strings. These variables are stored in two places; there’s a hidden pointer to the place in the heap where the data is stored.
– String
– All arrays, even if their elements are value types
– Class types, such as Form
– DelegatesImportant:
string str = “new string”;//1 address space
str = “modified” + str; //different address and not same address as above
value type is derived from System.ValueType while a reference type is derived from System.Object.
If you assign a value type variable to another then a direct copy of the value is made. But copying a reference type variable just makes a copy of the reference to the variable and does not affect the variable itself.
unlike a value type, a reference type can be null.
implicit conversion vs. explicit conversion 1. Explicit conversions require a cast operator where as an implicit converstion is done automatically.
2. Explicit conversion can lead to data loss where as with implicit conversions there is no data loss.
Will the following code compile?
double d = 9999.11;
int i = d;No, the above code will not compile. Double is a larger data type than integer. An implicit conversion is not done automatically bcos there is a data loss. Hence we have to use explicit conversion as shown below.double d = 9999.11;
int i = (int)d; //Cast double to int.
N.B: InvalidCastException occurs if casting fails
Boxing storing a value type variable in a reference variable
int i = 99;
object t=(object)i;
N.B: boxing happens implicitly
Unboxing storing a reference type variable in a  value type variable
object box = 12;
int i = (int)box;
N.B: – As performance gets impacted, try to avoid boxing and unboxing. In a project where you need boxing and unboxing, use it when it’s absolutely necessary.
– unboxing is an explicit conversion

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: