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:
AnnouncementI have released my new course on Udemy, Kubernetes By Example. Sign up now to get free lifetime access!
int MyFavouriteNumber = 7 ;
Here we are creating a integer variable called MyFavouriteNumber which has the value 7.
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 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.
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.:
static void Main()
Dog Charlie = new Dog();
// here we are triggering a nonstatic method.
// here we define a non-static method.
public void Woof()
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