c# – Static and Non-Static Methods

C# is a object oriented language, and that means that whenever we declare a new variable, we are actually using a class that generates (aka instantiates) that variable for us. Let’s says that somewhere in our program we have the following statement:

int MyFavouriteNumber = 7 ;

Here we are creating a integer variable called MyFavouriteNumber which has the value 7.

The word “int” is actually a c# builtin keyword, When using the int keyword before a variable’s name, we are essentially saying that we want this variable instantiated using the System.Int32 class.

As a result “MyFavouriteNumber” becomes a member of the System.Int32 class. This class has a number of methods. There a basically two types of methods, Static methods, and Non-static methods. Static methods are the ones indicated with the letter “S” label:

We will look at static methods later, but for now let’s look at non-static methods.

Non-Static Methods

Non-static methods are methods that you apply to an instance of a class, which is why they are also often called “instance methods”. It is a bit like doing experiments on an object.

Here’s an example, in which we are using the System.Int32 class’s “CompareTo(Int32)” method:

using System;

static class ComparingNumbersExample
{
    static void Main()
    {

        int MyFavouriteNumber = 7;
        Console.WriteLine (MyFavouriteNumber);  // this simply outputs "7"
        ComparisonResult = MyFavouriteNumber.CompareTo(2);
        Console.WriteLine (); // this outputs "1"
        Console.WriteLine (MyFavouriteNumber.CompareTo(15)); // this outputs "-1"
        Console.WriteLine (MyFavouriteNumber.CompareTo(7)); // this outputs "0"
        Console.ReadKey();  // this keeps the console open.
    }
}

line 8: Here we are using the CompareTo method on an instance of the String class, which in this case is the MyFavouriteNumber variable. We did this using a dot (.) notation format.

This program outputs a list of numbers, but what do these numbers mean? Well you can find this out by going to the “CompareTo” method’s reference page. Based on the reference info, we can derive:

line 10 – Here we are comparing our variable (i.e. MyFavouriteNumber) with the number 2. Since our variable is greater is than 2, it means that this method returns “1”.

line 11 – Here we are comparing our variable (i.e. MyFavouriteNumber) with the number 15. Since our variable is less than 15, it means that this method returns “-1”.

line 12 – Here we are comparing our variable (i.e. MyFavouriteNumber) with the number 7. Since they are the same value, it means that this method returns “0”.

You can also create your own non-static method, by simply omitting the “static” when declaring your method, e.g.:

using System;

namespace ANonStaticMethod
{
    class Program
    {
        static void Main()
        {
            Dog Charlie = new Dog();
			// here we are triggering a nonstatic method. 
			Charlie.Woof();
        }
    }

    class Dog
    {
		// here we define a non-static method.
		public void Woof()
		{
			Console.Beep();
		}
    }
}

Static methods

With non-static methods you may have to do a lot of work beforehand to create an instance of that class, and then apply the non-static method to it.

Static methods on the other hand are more general tools, and work right out-of-the-box without any need to create an instance of that class. These methods are indicated by the letter “S”.

We have already encountered a static method, called “WriteLine” which is a member of the

The Console.WriteLine is a perfect example of a static method. The WriteLine method is a static method that resides in the “Console” class, and if you want to use it to output something to the terminal

however in order to use it, we didn’t need to