Introduction to OOP in C#: Classes - Page 1
       by kirupa  |  5 January 2007

In the previous page we learned how to extend the functionality of our program by using methods. There are two types of methods - instance and static. We just finished discussing instance methods, and this page starts off by explaining static methods.

Static Methods
The other type of method you can use is called a static method. Unlike instance methods that could only be called from an object (ie: earth.GetName()), static methods can be called by polling the class directly.

Like before, let me first provide an example and then I will discuss the details. Add the following colored lines to your Planet definition:

class Planet
{
public int radius;
public double gravity;
private string name;
private static int count;
 
// A Constructor
public Planet(int r, double g, string n)
{
radius = r;
gravity = g;
name = n;
 
count++;
}
 
//Return the object's name!
public string GetName()
{
return name;
}
 
//Static method returning count of all planets
public static int GetCount()
{
return count;
}
}

The above changes are fairly minor. I first declare a static int field called count. In the constructor, I increment our count field by one by using the ++ operator. Second, I created a static method called GetCount() that returns the value stored by our count field.

To see what the code you added to the Planet class accomplishes, overwrite your main method with what I have provided below and run your program:

class Program
{
static void Main(string[] args)
{
Planet earth = new Planet(6378, 9.81, "Earth");
Planet saturn = new Planet(60268, 8.96, "Saturn");
 
Console.WriteLine("Total planets so far are: " + Planet.GetCount());
}
}

Notice that when you run your program, your output should be "Total planets so far: 2" That is, after all, the correct answer because we only have two planet objects created  - earth and saturn.

What is more interesting than the number of planets is how I call the GetCount() method: Planet.GetCount(). Instead of accessing GetCount() from either the earth or saturn objects, I am going straight to the Planet class itself. That outcome is a result of tagging the GetCount() method with the static modifier.

If this is your first encounter with a static method or field, the following diagram should help you get an idea of how static and instance methods/fields are represented in a program:

As you can see from the dotted lines in the above image, static variables and methods are shared among all objects. Even though I have two objects called earth and saturn, only one copy of the count field and getCount() method are generated throughout the entire program.

If you remove the static modifier, you will only be able to access the getCount() method via an object, for example - earth.GetCount(). Also, the data will be localized to just your object as seen by the instance variables and GetName() method in the blue Saturn and Earth boxes.

AutoComplete in Visual Studio
Up until now, I gave you the impression that you had to know beforehand which methods and fields were public or private before attempting to access them. In Visual Studio, the Auto-Complete feature does a good job exposing the public fields that you can access when you simply type the name of your object followed by the a . dot/period:

[ the Auto-Complete drop-down displays the fields and methods you can access ]

As you can see from the above image, I am able to see all of the methods and fields from the Planet class that I can access. You will see more methods than the gravity, radius, and GetName fields and methods you created. These extra entries such as GetType, Equals, GetHashCode, and ToString are part of the default methods that almost all classes automatically have.

Conclusion
If you are familiar with how object-oriented programming (OOP) works in other languages such as Java, much of this tutorial must have been a review for you. If this is your first introduction to OOP, this tutorial covered a lot of ground, and I introduced a lot of terminology that you will encounter in the real world. To best learn this material, play around and create some simple applications. A tutorial can only give you the basic idea of how something works, and its up to you to extend what you learned and apply it in your own programs.

Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!

When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.

Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.

COMMENTS

 

1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.