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
Updated on: 2020-06-20T13:09:32+05:30

698 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements