What are static member functions in C#?

CsharpProgrammingServer Side Programming

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
raja
Published on 08-Aug-2018 07:37:52
Advertisements