Sunday, December 25, 2011

out and ref parameters

Dot net allows to pass the arguments in three ways.Here i will show how to use these parameters
1.call by Ref:Formal arguments are modified and if modification are reflected on actual arguments .This concepts are called as call by value.By default value the variable will be passed by Value.The Ref is a keyword which is required to pass the variable by ref.This keyword must be used along with actual and formal arguments.Ref variable must be initiated before passing the function.The fallowing example will show how to use this ref key word
Example:
class test
  {
    public void swap(int x,ref int y)
      {
        int t=x;
        x=y;
        y=t;
      }
   public void button1_click(object sender O,Event Args e)
     {
      test t=new test();
      int a=10,b=20;
      t.swap(a,ref b);
      message.show(a+""+b); 
     }
2.call by value:Formal arguments are modified and if modification are not reflected on actual arguments .This concepts are called as call by value
Example:
class test
  {
    public void print(int x)    
      {
        x=x+x;
      }
   public void button1_click(object sender O,Event Args e)
     {
      int a=10; 
      test t=new test();
      t.print(i);
      message.show(i); 
     }
3.call by out:The ref and out parameters are most same.Ref variable must be initialized before passing but Out variable passed with out initialized also.Even if Out variable is initialized.The value will not be passed.Finally
out=ref-initialization
Example:
class test
  {
    public void print(int a,int b,out int c)
      {
        c=a+b;
      }
   public void button1_click(object sender O,Event Args e)
     {
      int a=10,b=20,k;
      test t=new test();
      t.print(i,j,out k);
      message.show(k+""); 
     }

No comments:

Bel