Day 18: Instance versus Static Members

Leslie Ramírez
2 min readDec 29, 2021

--

Hello there!! 👋👋👋 It’s our 18th of coding winter 2021, This time I’m going to share with you about the difference between instance and static members. Let’s go!

Static Members

The static member is callable on class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. A non-static class can contain static methods, fields, properties or, events.

You declare static class members by using the static keyword before the type of the member, as the following example:

General Features

  • Static methods can be overloaded but not overridden, because they belong to the class, and not an instance of the class.
  • C# does not support static local variables.
  • Only one copy of a static member exists, regardless of how many instances of the class are created

Instance Member

Before we define our instance member we should know class are references types, a variable of a class object holds a reference to the address of the object. the instances of classes are created by using new operator:

Unicorn object1 = new Unicorn();

Instance versus static members

  • The members of a class that must be accessed by creating an instance for the class are called as instance members and the members of a class that can be accessed without creating an instance are called as static members.
  • No matter how many instances we create, there will only be one copy of a static field.

Conclusions

The static members are shared with all objects of the class, but instance members belong to the object that they are instantiated from. If you want to learn more about it follow this link:

If you enjoyed this article, please hold down the clap button 👏👏👏 Don’t forget to follow our Coding winter Calendar. Have a wonderful day! 🧡 💛 💚🧡 💛 💚

--

--

Leslie Ramírez

I am Microsoft MVP in the award category: Developers technologies, a professional with several years in software development specifically in .NET technology.