……………………………………………….Expertise in .NET Technologies

Methods in C# (OOP’s) – Part 5

Posted by Ravi Varma Thumati on July 13, 2009

Methods in C#

Methods are constructions that break down the large sections of the code into small unit. Methods perform specific tasks, and usually hides the entire operations [encapsulate] from the user.

  • Maintain and modify the code easily.
  • Understand the code.
  • Make the code readable, reliable, and reusable.
  • Avoid code redundancy.
  • Easy determine the exceptions and correct them.

What is a Method?

A method is simply a discrete piece of code that can be called by other functions. When the method is called, control of the program is passed to the method, which performs its activities before returning control back to the command following the calling statement. Often methods call other methods in a nested arrangement. As each method completes its execution control returns to the point of the previous call until eventually the line of code after the original calling statement is reached.

The use of methods provides several benefits. The key advantage is that code that would otherwise be repeated can be held in a single location. If the code for a particular function exists in one easy-to-find subroutine, modifications and bug fixes are much simpler than if the same code has been duplicated in many places. A second benefit is that when a method is given a descriptive name, it can make your source code easier to understand. This helps in the creation of self-documenting code that requires fewer comments due to its simplicity.

Creating a Method

In this section we will create a simple method that obtains the current date and time. The date part will be formatted and then outputted to the console. Although the functionality is very simple, it serves as a good demonstration of the structure of a method and illustrates how the code could be modified centrally should, for example, the formatting of all outputted dates need to be changed throughout a large program.

Naming a Method

Methods can be named using the same rules as variables. Upper and lower case letters, numeric digits and the underscore (_) character may be used. Although any combination of these characters is permissible, methods are usually given descriptive names that indicate the action that they perform. A common standard to follow is to use Pascal Case for the method name. This means that the first letter of the method name is capitalised and the remaining text is lower case. Where several words are joined together, each word begins with a capital letter.

Using these conventions, good names for methods could include:

  • Delete
  • DeleteFile
  • ConnectToDatabase
  • OutputFormattedDate

The simplest type of method is one that performs a task without requiring any parameters and without returning any information on completion. This is the type of method that will be created in this article with more complex methods being investigated in a later instalment of the tutorial.

Declaring a method is very similar to defining a variable. The type of the return value of the method is followed by the method name. If there is no return value, the method’s type is set to void. As we are using no parameters, an empty pair of parentheses () is then appended. The code to declare our example method is therefore as follows:

void OutputFormattedDate()

To use this example, create a console application and add the code within the braces of the class but outside of the ‘Main’ method. The full example can be found at the end of this article.

Adding Code

Once the method is declared, it needs a code block adding to it. Like all code blocks, the series of commands is surrounded by brace characters, {}. The following example adds the code to output the formatted date to the console.

void OutputFormattedDate()


DateTime theDate = DateTime.Now;



Returning From a Method on Completion

The example method above simply determines the current date and outputs it. After the Console.WriteLine command the method has no further code to execute so control naturally returns to the point after the method was originally called. However, it is possible to instruct the program to return from a method at any point by adding a return statement. The following example shows this by returning without outputting to the console date if it is Christmas day.

void OutputFormattedDate()


DateTime theDate = DateTime.Now;

if (theDate.Day == 25 && theDate.Month == 12)




The return command may be used several times within a method to exit at different points according to your requirements. However, the number of return points should be kept to a minimum in order to keep the code as easy to understand as possible. In ideal conditions there should be only one return statement or the code should end naturally when control reaches the closing brace character.

Calling a Method

Now that the method is complete it can be called. As with other methods examined through the course of this tutorial, it belongs to a class. If you are using a standard console application, this class will be called Program and will now contain a Main method as well as OutputFormattedDate. To use the new method, you must first create a new Program object and call its OutputFormattedDate function. This can be controlled within the Main method making the final code as follows:

class Program


static void Main(string[] args)


Program p = new Program();



void OutputFormattedDate()


DateTime theDate = DateTime.Now;








Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: