Method Under Class (Call by Value & Call by Reference)


Next Articles - "ReadOnly", "Ref", "Out", "Named" Parameter's & "this"

For Example :
 Access Modifier void|type < name >( Parameter Define's )
  {
      statements 
  }

 i.e : Access modifier    ---  public, private, protected, virtual, abstract..
       Paramter Define's  ---  ref parameter, out parameter, this keywords, named parameters..


  • A method referred as a sub-program is a block of code that can be reused. We can categorize them into value returning and Non value returning methods.
  • Modifiers or some special keywords which can be used on a method like Public, Private, Virtual, Abstract etc.
  • Void | type : we used void to a method to specify it was non- value returning where as if the method is value returning using the type we need to tell the type of value it was returning.

    Note : Type need not be only predefined data types like int, float, bool etc. that can also be class type also.

  • Using the optional parameter "Parameter Define's" we can pass parameters to a method as following.
    [ref |out] < var> [ = value] [,…n]

    A method can be defined with any number of parameters, where these parameters are divided into two types.

1.) Input Parameter

2.) Output Parameter


1.) Input Parameter

This kind of parameter is specify to give the same input to method for calling it.
There are two type of parameter

  1. Call by value
  2. Call by reference
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.

Difference Between Value Type & Reference Type

Value Type

Reference Type

Value type store in Stack Memory Reference type store in 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 can be initialize with zero(0) reference type initialize with NULL
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 .
Tag : C#, C# Tutorials
0 Komentar untuk "Method Under Class (Call by Value & Call by Reference)"

Back To Top