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

Written by mendelbak | Published 2018/03/14
Tech Story Tags: programming | c | software-development | learning-to-code | csharp

TLDRvia the TL;DR App

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 Classpublic static class exampleStaticClass{}

// Static Methodpublic 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.

// Classpublic class exampleClass{}

// Instance Methodpublic 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) classpublic class exampleClass{}

// Creating an instance of the classexampleClass 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 classpublic static class exampleStaticClass{// Declaring a static methodpublic 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 workexampleStaticClass.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) classpublic class exampleClass{// Declaring a static methodpublic static void str exampleStaticMethod(){Console.WriteLine("Static Methods are allowed in both non-static and static classes.");}

// Declaring a instance methodpublic void str exampleInstanceMethod(){Console.WriteLine("Instance Methods are allowed only in non-static classes");}

}

// Creating an instance of the classexampleClass myNewInstance = new exampleClass();

// Calling the methods on the instance of the class.myNewInstance.exampleInstanceMethod(); // Would workmyNewInstance.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.


Published by HackerNoon on 2018/03/14