What are static member functions in C#?


Static functions can access only static variables. The static functions exist even before the object is created.

Set static functions as −

public static int getNum() {}

The following is an example demonstrating the use of static functions −

Example

 Live Demo

using System;

namespace Demo {
   class StaticVar {
      public static int num;

      public void count() {
         num++;
      }

      public static int getNum() {
         return num;
      }
   }

   class StaticTester {
      static void Main(string[] args) {
         StaticVar s = new StaticVar();

         s.count();
         s.count();
         s.count();

         Console.WriteLine("Variable num: {0}", StaticVar.getNum());
         Console.ReadKey();
      }
   }
}

Output

Variable num: 3

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements