You are on page 1of 4

Awesome c sharp : value type, reference type, command line argument by santosh

value type, reference type, command line argument


by santosh Welcome all

Hola... [hi...] friends [Namastey :- i welcome u all in awesome c sharp]. i welcome all my new readers and my returned friend. i m back with my new post on awesome c sharp. As i promised in my previous post that i will talk about value type, reference type andcommand line argument in c sharp so here is my part. i was listing some awesome music (classic hits) it feels great relax, listing music with little bit of work. U can also try that, now move on to our work. Note: please let the images to be load it will take few seconds

Using Methods with parameter.

I know friends u must be thinking that what is the use of methods here, so time has came to understand functions with parameter so that concept of value type, reference type can be implemented in our program. Friends parameter allow information to be passed in and out of a method . When u define a method. u can include a list of parameter in parentheses. for more information on functions please visit http://awesomecsharp.blogspot.in/2013/06/awesome-c-sharp-about-functions-and.html Declaring methods with parameters : Each parameter has a type and a name. you can declare parameters by placing parameter declaration inside parentheses. A syntax used to declare parameter is similar to the syntax used to declare local variables, u can separate each parameter declaration with a comma. Following example show the declaring of the method with parameter. void Function_Name(int n, string str) { statement; }

preceding code declares a function any name can be given to Function_Name which consist two parameter one integer type another string type. Now lets see calling of methods with parameter type. When a method with parameters is called, you must pass a parameters, to the method. Parameter can be passed by using one ofthe following ways.

(Value :)
Our value type :- Are sometimes called in parameter. Therefore data can be transferred into the method but cannot be transferred out.

Explanation : This is very simple code in which i have a function whose name is Accept [user defined function] containing two integer variable as parameter. name num and numbo. Always remember calling of program is always started from Main function ( Main function() ) So we will also stat from Main function() in which we will be taking 2 integer input from user to make our program much useful for user. Now num1 and num2 will receive value user. and later on i have created object of the class named it C i.e Car C= new Car(); as already defined in my previous post new keyword provide memory for C object of class and Car(); is a constructor will define it in future post. After initialize of c object it will invoke Accept in which as a parameter variable num1 and num2 is passed. Now Accept function will invoke and in Accept function which will receive value of num1 and num2 in num and numbo after that value of num and numbo will be added and passed to sum variable later on sum will be displayed as result of the code. Point to note is that while In value type we have actual value in the variable i.e they have their own value if any change is made in any variable value it will not effect in another variable. Let see the code using System; class Car { public void Accept(int num, int numbo) { int sum = num + numbo; Console.WriteLine("Sum of your input number is : "+sum); } public static void Main() { Console.WriteLine("please enter a number to add"); int num1 = int.Parse(Console.ReadLine()); Console.WriteLine("please enter a number to add"); int num2 = int.Parse(Console.ReadLine()); // now we will call Accept method with car class object Car C = new Car(); C.Accept(num1, num2); Console.Read(); } } for making your program code more dynamic please read user interaction post http://awesomecsharp.blogspot.in/2013/05/awesome-c-sharp-user-interaction.html
(Reference type)

Reference type :- A reference type parameter is the reference to the memory location of a data member. Unlike a value type, A reference type parameter does not create a new new memory location, instead of creating new memory location it takes reference ofthe same memory location. If any change is made in variable then the reference variable will reflect that change which is not possible in value type.

here in this pic variable number is actual variable resource which contains value 6 in the actual memory, when Add method is applied which will increment value from 6 to 7 in variable then the reference variable value is also get modified to 7 and if any change is made any where it will modify all reference variable value. For making a variable reference type ref keyword is used.

Lets see its code using System; class Car { public void Accept(ref int numbo) { numbo++; } public static void Main()

{ int number = 6; Car C = new Car(); C.Accept(ref number); Console.WriteLine("i have incremented numbo not number how number got increment "+number); }}

For complete article please visit

http://awesomecsharp.blogspot.in/2013/06/awesome-c-sharp-value-type-reference.html

You might also like