@ 2010 Tata McGraw-Hill Education
1
Education
Methods in C#Methods in C#
@ 2010 Tata McGraw-Hill Education
2
Education
Introduction
In object-oriented programming, objects are used as building blocks in
developing a program. They are the runtime entities. They may represent a
person, a place, a bank account, a table of data or any item that the program
handles. Objects encapsulate data, and code to manipulate that data. The
code designed to work on the data is known as methods in C#. Methods give
objects their behavioral characteristics. They are used not only to access and
process data contained in the object but also to provide responses to any
messages received from other objects. C# is a pure object-oriented language
and therefore, every method must be contained within a class Remember, in
other languages like VB, C and C++, we can defi ne global functions that are
not associated with a particular class. In this chapter, we shall see how
methods are defined and used in different situations. More about their
applications in the development of object-oriented system will be discussed in
Chapter 12.
@ 2010 Tata McGraw-Hill Education
3
Education
DECLARING METHODS
Methods are declared inside the body of a class, normally after the declaration of data
fields. The general form of a method declaration is
modifiers type methodname (formal-parameter-list)
{
method body
}
Method declaration has five parts:
Name of the method (methodname)
Type of value the method returns ( type )
List of parameters ( formal-parameter-list )
Body of the method
Method modifiers ( modifier )
@ 2010 Tata McGraw-Hill Education
4
Education
THE MAIN METHOD
As mentioned earlier, C# programs start execution at a method named Main( ). This
method must be the static method of a class and must have either int or void as
return type
public static int Main( )
Or
public static void Main( )
The modifier public is used as this method must be called from outside the program.
The Main can also have parameters which may receive values from the command line
at the time of execution.
public static int Main (string [ ] args)
public static void Main (string [ ] args)
@ 2010 Tata McGraw-Hill Education
5
Education
INVOKING METHODS
Once methods have been defined, they must be activated for operations. The process
of activating a method is known as invoking or calling. The invoking is done using the
dot operator as shown below: objectname.methodname( actual-
parameter-list );
NESTING OF METHODS
We mentioned earlier that a method of a class can be invoked only by an object of that
class if it is invoked outside the class. We have also seen an exception where a method
is invoked without using any object and dot operator. That is, a method can be called
using only its name by another method of the same class. This is known as nesting of
methods
@ 2010 Tata McGraw-Hill Education
6
Education
METHOD PARAMETERS
A method invocation creates a copy, specific to that invocation, of the formal
parameters and local variables of that method. The actual argument list of the
invocation assigns values or variable references to the newly created formal
parameters. Within the body of a method, formal parameters can be used like any other
variables.
PASS BY VALUE
By default, method parameters are passed by value. That is, a parameter declared with
no modifier is passed by value and is called a value parameter. When a method is
invoked, the values of actual parameters are assigned to the corresponding formal
parameters
@ 2010 Tata McGraw-Hill Education
7
Education
PASS BY REFERENCE
We have just seen that pass by value is the default behaviour of methods in C#. We can,
however, force the value parameters to be passed by reference. To do this, we use the
ref keyword. A parameter declared with the ref modifi er is a reference parameter.
Example:
void Modify ( ref int x )
THE OUTPUT PARAMETERS
As pointed out earlier, output parameters are used to pass results back to the calling
method. This is achieved by declaring the parameters with an out keyword. Similar to
a reference parameter, an output parameter does not create a new storage location.
@ 2010 Tata McGraw-Hill Education
8
Education
VARIABLE ARGUMENT LISTS
In C#, we can defi ne methods that can handle variable number of arguments using what
are known as parameter arrays. Parameter arrays are declared using the keyword
params.
METHODS OVERLOADING
C# allows us to create more than one method with the same name, but with the
different parameter lists and different definitions. This is called method overloading.
Method overloading is used when methods are required to perform similar tasks but
using different input parameters.

Methods in C#

  • 1.
    @ 2010 TataMcGraw-Hill Education 1 Education Methods in C#Methods in C#
  • 2.
    @ 2010 TataMcGraw-Hill Education 2 Education Introduction In object-oriented programming, objects are used as building blocks in developing a program. They are the runtime entities. They may represent a person, a place, a bank account, a table of data or any item that the program handles. Objects encapsulate data, and code to manipulate that data. The code designed to work on the data is known as methods in C#. Methods give objects their behavioral characteristics. They are used not only to access and process data contained in the object but also to provide responses to any messages received from other objects. C# is a pure object-oriented language and therefore, every method must be contained within a class Remember, in other languages like VB, C and C++, we can defi ne global functions that are not associated with a particular class. In this chapter, we shall see how methods are defined and used in different situations. More about their applications in the development of object-oriented system will be discussed in Chapter 12.
  • 3.
    @ 2010 TataMcGraw-Hill Education 3 Education DECLARING METHODS Methods are declared inside the body of a class, normally after the declaration of data fields. The general form of a method declaration is modifiers type methodname (formal-parameter-list) { method body } Method declaration has five parts: Name of the method (methodname) Type of value the method returns ( type ) List of parameters ( formal-parameter-list ) Body of the method Method modifiers ( modifier )
  • 4.
    @ 2010 TataMcGraw-Hill Education 4 Education THE MAIN METHOD As mentioned earlier, C# programs start execution at a method named Main( ). This method must be the static method of a class and must have either int or void as return type public static int Main( ) Or public static void Main( ) The modifier public is used as this method must be called from outside the program. The Main can also have parameters which may receive values from the command line at the time of execution. public static int Main (string [ ] args) public static void Main (string [ ] args)
  • 5.
    @ 2010 TataMcGraw-Hill Education 5 Education INVOKING METHODS Once methods have been defined, they must be activated for operations. The process of activating a method is known as invoking or calling. The invoking is done using the dot operator as shown below: objectname.methodname( actual- parameter-list ); NESTING OF METHODS We mentioned earlier that a method of a class can be invoked only by an object of that class if it is invoked outside the class. We have also seen an exception where a method is invoked without using any object and dot operator. That is, a method can be called using only its name by another method of the same class. This is known as nesting of methods
  • 6.
    @ 2010 TataMcGraw-Hill Education 6 Education METHOD PARAMETERS A method invocation creates a copy, specific to that invocation, of the formal parameters and local variables of that method. The actual argument list of the invocation assigns values or variable references to the newly created formal parameters. Within the body of a method, formal parameters can be used like any other variables. PASS BY VALUE By default, method parameters are passed by value. That is, a parameter declared with no modifier is passed by value and is called a value parameter. When a method is invoked, the values of actual parameters are assigned to the corresponding formal parameters
  • 7.
    @ 2010 TataMcGraw-Hill Education 7 Education PASS BY REFERENCE We have just seen that pass by value is the default behaviour of methods in C#. We can, however, force the value parameters to be passed by reference. To do this, we use the ref keyword. A parameter declared with the ref modifi er is a reference parameter. Example: void Modify ( ref int x ) THE OUTPUT PARAMETERS As pointed out earlier, output parameters are used to pass results back to the calling method. This is achieved by declaring the parameters with an out keyword. Similar to a reference parameter, an output parameter does not create a new storage location.
  • 8.
    @ 2010 TataMcGraw-Hill Education 8 Education VARIABLE ARGUMENT LISTS In C#, we can defi ne methods that can handle variable number of arguments using what are known as parameter arrays. Parameter arrays are declared using the keyword params. METHODS OVERLOADING C# allows us to create more than one method with the same name, but with the different parameter lists and different definitions. This is called method overloading. Method overloading is used when methods are required to perform similar tasks but using different input parameters.