Ad Code

Responsive Advertisement

Defining Method under classes –

Defining Method under classes 

[< modifiers >]void|type <name>([<param defn’s>])
{
-stmts

  •  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 “param defn’s” we can pass parameters to a method as following.
                                       [ref |out] <type> < var> [ = value] [,…n]
                                                    .----------------------------
A method can be defined with any number of parameters, where these parameters are divided into two types.

1. input parameters

2. Output Parameters.

  • Input parameters are used for tacking a value into the method for execution, whereas output parameters are used for returning the value out of the method after execution.
  • To tack input parameter we used pass by value” mechanism and for output parameter we used “pass by reference” mechanism, which is similar to pass by address in “C” language.
Note – both “pass by address and pass by reference “uses the concept of pointer but “pass by address is explicit pointer” where address are managed by programmer, whereas “pass by reference is implicit pointer” where address are managed by garbage collector implicitly.
  • To pass a parameter using pass by reference mechanism it should be prefixed with either “ref | out “keywords.
  •  [ = value ] - In C# 4.0 we are giving with a feature named and optional parameter, which allows defining a parameter with a default value while calling the method passing values to that parameter becomes optional.
Reactions

Post a Comment

0 Comments