Declare a member function as static. Such functions can access only static variables. The static functions exist even before the object is created.
##A static in#n. and can only contain static members.Static methods is set using static keyword −public static int getNum() { return num; }
using System; namespace StaticVarApplication { 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(); s.count(); s.count(); s.count(); Console.WriteLine("Variable num: {0}", StaticVar.getNum()); Console.ReadKey(); } } }
以上是C# 中的靜態方法與非靜態方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!