Access to Class Members
The last point I'll make about static methods is the rule governing which class members can be accessed from a static method. As you might imagine, a static method can access any static member within the class, but it cannot access an instance member. This is illustrated in the following code: -
using System;
class SQLServerDb
{
static string progressString1 = "repairing database...";
string progressString2 = "repairing database...";
public static void RepairDatabase()
{
Console.WriteLine(progressString1); // This will work.
Console.WriteLine(progressString2); // Fails compilation.
}
}
class StaticMethod3App
{
public static void Main()
{
SQLServerDb.RepairDatabase();
}
}