Method Parameter in C#

0

C# is having 4 parameter types which are:

  1. Value Parameter : Default parameter types.Only Input
  2. Reference (ref) Parameter : Input / Output
  3. Output (out) Parameter 
  4. Parameter (params) Arrays

Value Parameter

     Are used for passing parameters into methods by value.When a method is invoked, the values of actual parameter are assigned to the corresponding formal parameter. The values can be changed with in the method. The value of the actual parameter that is passed by value to a method is not changed made to the corresponding formal parameter within of the method. This is because the methods refer to only copies of those variables when they are passed by value.

 

Reference Parameter (Pass By Reference)

     Used to pass parameters, a reference parameter does not create a new storage location. Instead it represents the same storage location as the actual parameter used in the method invocation. Reference parameter is used in situations where we would like to change the values of variables in the calling method.

 

Output Parameter

     Used to pass results back from a method. Achieved by declaring the parameter with an out keyword. Doesn’t Create a new storage location. Instead,becomes an alias to the parameter in the calling method. When a formal parameter is declared as out, the corresponding actual parameter in the calling method. Must also be declared as out.

 

Parameter Arrays

     Used in method definition to enable it to receive variable no of arguments when called. Declared using the keyword params. Not allowed to combine the params modifier the ref and out modifiers.




If you like this article then put valuable comments.

 
 

Difference Between Const and Readonly in C#

0

The read only keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A read only field can be initialized either at the declaration or in a constructor. Therefore read only field can have different values depending on the constructor used. Also, while a const field is a compile time constant but the read only field can be used for run time constants.