स्टैटिक फंक्शन्स केवल स्टैटिक वेरिएबल्स को एक्सेस कर सकते हैं। स्टैटिक फंक्शन्स ऑब्जेक्ट बनने से पहले भी मौजूद होते हैं।
स्टैटिक फंक्शन्स को इस तरह सेट करें -
public static int getNum() {}
स्टैटिक फंक्शन्स के उपयोग को दर्शाने वाला निम्नलिखित उदाहरण है -
उदाहरण
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();
}
}
}
आउटपुट
Variable num: 3