Declaring methods inside classes is great but what do you do when you want a method you can call anytime? If you’re using C++, then just write a method. But if you still want your method in a class or if you’re using C# where all methods need to be in classes, then you’ll need to make your method static. And you can declare static data too. What does that do?

This episode describes a class called string with a static method called isNullOrEmpty which takes a single parameter of type string and returns true if the passed string is null or if it’s empty. If the passed string is valid and has characters, then this method returns false.

If you have some code outside of the string class and declare a string variable like this:

C++ code:
string * name = someMethodThatMightReturnAString();
if (string::isNullOrEmpty(name))
{
// Do something else because we don’t have a string.
}

C# code:
String name = SomeMethodThatMightReturnAString();
if (String.IsNullOrEmpty(name))
{
// Do something else because we don’t have a string.
}

Notice how there are slight differences between the two examples due to pointers vs. references and due to normal naming differences between C++ and C#. C++ also uses two colons between the class name and the static method while C# uses a dot.