Parameter-we can pass two type of parameter to the method.
Step 1 - First open your visual studio->File->New->Project->console Application->OK->write the program which is below:
see it
- Input parameter
- Out parameter
- Input parameter- This kind of parameter is specify to give the same input to method for calling it.
- Call by value -> In this case when we call the method of any class (which takes some parameter) from main method using object.Then value of parameter in main method will directly copy to the class method to parameter values respectively. In this case if some changes occurs in values within the method that change not occurs in actual variable .I have full describe this concept through programming which is given below.
- Call by reference -> In this case when we call the method,the reference address of variable is passed to the method.If some changes occurs in values within the method that changes occurs in actual variable.To specify this parameter we use 'ref' Keyword at the time of parameter declaration as well as the calling method.
Step 1 - First open your visual studio->File->New->Project->console Application->OK->write the program which is below:
see it
using System;
namespace callbyvalue
{
class Program
{
public class employee
{
public void display(int a, String b)
{
Console.WriteLine("Integer value is"+" " +a);
Console.WriteLine(" String value is" + " " + b);
Console.ReadLine();
}
}
public class student
{
public void show(ref String str)
{
Console.WriteLine("Enter the value");
string s = Console.ReadLine();
str = str + s;
Console.WriteLine("value in str variable is"+" "+str);
Console.ReadLine();
}
}
//all class member is called through main method.
static void Main(string[] args)
{
//creating the object of employee class first and implementing the call by value concept.
String m = "sunil";
employee emp = new employee();
emp.display(200,m);
Console.WriteLine("value in variable m is" +" "+m);
Console.ReadLine();
//creating the object of employee class first and implementing the call by Reference concept
string msg="Hello";
student st = new student();
st.show(ref msg);
Console.WriteLine("value in msg is" +" "+msg);//value at address msg will be print,because here address is copy not value thatswhy at same address value will be print
Console.ReadLine();
}
}
}
Step 2 - Now Run the program(press F5).
Output:
Description:-There are some steps to describe the whole program.
- First i have created a employee class ,In employee class i have taken a display method which takes two parameter.Ex display(int a, String b).On this class i have implemented call by value concept.
- After that i have created a student class .In student class i have taken a show method which takes one parameter with ref keyword.This class is used for implementing the call by reference concept in c#.
- After that i have created object of employee class in main method,here i have define a variable m which holds the sunil value .after that i called display method using object.EX. emp.display(200,m), these value directly pass to the display method. see it. a <--200, b <--m<--sunil and display method will print a and b values,if we print m value then sunil will print because here value is copy not reference address.
- After that i have created object of student class in main method and called student class method by the object.Ex st.show(ref msg);here reference address of msg will copy to the str variable.If we print msg value in main method then different value will print which is calculated by the show method in student class.
- In call value actual value is copy to the method but In call by reference reference address of value is copy to the method.
There are following difference between value type and reference type on the basis of storage.
- Value type store within stack memory and reference type store within heap memory.
- Structure,all primitive data type except string,enum are the example of value type.Class,string,array,delegate,interface is the example of reference type.
- Value type directly store values within stack but reference type contains reference object of value which store in heap.
- when value type is copy to another value type then actual value is copy,but when reference type is copy to another reference type then reference address of value is copy .
- Value type can be initialize with zero(0) and reference type initialize with NULL.
Note:- Output parameter concept i will discuss next tutorial
For more...
For more...
- How to host asp.net website on server free
- How to host asp.net application on IIS server
- String and string builder in c#
- Delegate in c#
- File Handling Real application
- How to add controls at run time in .NET Application
- How to implement Form based authentication in asp.net
- How to use web services in asp.net application
- How to send mail from asp.net website free
- How to implement cookies in asp.net application
- How to make composite custom controls in .NET
- How to create captcha image without dll file
- How to handle date in sql server
- How to implement Reflection concepts
I hope this helpful for you.if any problem please comment it, i will improve it as soon as possible.
To Get the Latest Free Updates Subscribe
To Get the Latest Free Updates Subscribe
Click below to download whole application.
awesome. i didn't see this much of explain before.
ReplyDeleteawesome.....for reading this explain clear my concept ..........
ReplyDeletethanks keep it up
thanks yaar
ReplyDeleteawesome explanation
ReplyDeleteReally simple and good explanation. :)
ReplyDeleteThanks a lot sir for this awesome explanation :D
ReplyDeleteVery helpfull
ReplyDelete