Lesson 3.1 Simple Void Method

The best way to describe this method is to show it in action.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Methods1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Start Time:");

            System.Console.Write("*************");
            System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
            System.Console.WriteLine("*************");

            for (int x = 0; x <= 100000000; x++)
            {
            }

            System.Console.WriteLine("End Time:");

            System.Console.Write("*************");
            System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
            System.Console.WriteLine("*************");

            System.Console.ReadKey(true);


        }
    }
}
 

This program times how long it takes your computer to count to 100 million. As it turns out, that’s not very long. It will differ from machine to machine but mine takes about a quarter of a second. The output looks like this.

Counting time stamps

It shows a start time stamp and an end time stamp. The difference between the two is the amount of time it took to count to 100 million.

Note that there is a piece of code that puts a time stamp before and after the code.

            System.Console.Write("*************");
            System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
            System.Console.WriteLine("*************");
 

Bit of duplication going on there. The same block is used twice. This is where methods come to the rescue.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Methods2
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Start Time:");
            ShowTimeStamp();

            for (int x = 0; x <= 100000000; x++)
            {
            }

            System.Console.WriteLine("End Time:");
            ShowTimeStamp();

            System.Console.ReadKey(true);

        }

        static void ShowTimeStamp()
        {
            System.Console.Write("*************");
            System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
            System.Console.WriteLine("*************");

        }
    }
}
 

What I have done is take the bit of code that showed the time stamp and placed it into its own method and called it ShowTimeStamp.

static void ShowTimeStamp()
{
    System.Console.Write("*************");
    System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
    System.Console.WriteLine("*************");
}
 

I have then replaced those lines in the original code with calls to this method.

ShowTimeStamp();

If you run both types of program, you will notice that the output is identical. I have altered the workings of the code without its output. This is also referred to as refactoring and is a very important concept.

So what does the void mean? What have the empty () brackets got to do with anything? The answer to these questions and more are in the next two sections.

 


Supporting files

File Description Open with
Methods 1 Shows the code before the method has been made Visual C# 2008
Methods 2 Shows the code after the method has been added Visual C# 2008