स्थैतिक कार्य केवल स्थिर चरों तक पहुँच सकते हैं। स्थिर कार्य वस्तु के बनने से पहले ही मौजूद होते हैं।
स्थिर कार्यों को −
. के रूप में सेट करें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