Mendel Bakaleynik

@mendelbak

C#: Static vs Non-Static Classes and Static vs Instance Methods

C#: Static vs Non-Static Classes and Static vs Instance Methods

C# methods and classes can have several statements in their declarations. You might be used to seeing them written something like this:

// Static Class
public static class exampleStaticClass
{}
// Static Method
public static int exampleStaticMethod()
{
return 1;
}

Today we’re going to focus on the “static” vs “instance” keyword.

Firstly, the static keyword differentiates it from an instance method or class.

// Class 
public class exampleClass
{}
// Instance Method
public instance int exampleInstanceMethod ()
{
return 1;
}

Note: Classes are always classes that can be instantiated (non-static) unless specified to be static classes.

Since C# is an Object Oriented Language we quite frequently use instantiated objects derived from a class.

// Defining the (non-static) class
public class exampleClass
{}
// Creating an instance of the class
exampleClass myNewInstance = new exampleClass();

Next, we’re going to talk about applying those methods to a class or the instances of a class.

Methods

Methods that are declared using the static keyword will allow you to call that method onto a static class itself, versus onto the instance of a class.

Conversely, if your method was declared using the instance keyword you would be able to apply that method to only instantiated objects of that class and not onto the class itself.

Here’s an example using a static class:

// Declaring a static class
public static class exampleStaticClass
{
// Declaring a static method
public static void str exampleStaticMethod()
{
Console.WriteLine("Static Methods are allowed in static classes.");
}

// Declaring a instance method
public void str exampleInstanceMethod()
{
Console.WriteLine("Instance Methods are not allowed in static classes");
}
}
// Calling the methods on the class.
exampleStaticClass.exampleStaticMethod(); // Would work
exampleStaticClass.exampleInstanceMethod(); // Would not work

Not only that, but while non-static classes may contain both static and instance methods, static classes are only allowed to have static methods.

However, those static methods inside the instance class can not be called on the instance but only on the class itself.

Here’s an example using a non-static class:

// Declaring a (non-static) class
public class exampleClass
{
// Declaring a static method
public static void str exampleStaticMethod()
{
Console.WriteLine("Static Methods are allowed in both non-static and static classes.");
}
// Declaring a instance method
public void str exampleInstanceMethod()
{
Console.WriteLine("Instance Methods are allowed only in non-static classes");
}
}
// Creating an instance of the class
exampleClass myNewInstance = new exampleClass();
// Calling the methods on the instance of the class.
myNewInstance.exampleInstanceMethod(); // Would work
myNewInstance.exampleStaticMethod(); // Would not work
// Calling the static method on the instance class itself (not the instance of the class but the class itself)
exampleClass.exampleStaticMethod(); // Would work

Recap

  • Non-static (“regular”) classes can be instantiated.
  • Static classes cannot be instantiated.
  • Non-static classes can have instance methods and static methods.
  • Static classes can only have static methods.
  • Instance methods must be called on the instances of the class, not the class itself.
  • Static methods must be called on the class itself, not on the instances of the class.

If you enjoyed this article please share it or applaud in order to help others to find it.

If you have any questions or comments, please leave a comment below.

More by Mendel Bakaleynik

Topics of interest

More Related Stories