static的使用
1 class Program
2 {
3 static void Main(string[] args)
4 {
5
6 GreetPeople("Mingle",Language.English);
7 GreetPeople("明高",Language.Chinese);
8 Console.ReadLine();
9 }
10
11 public static void GreetPeople(string name,Language lang)
12 {
13 switch (lang) {
14 case Language.English:
15 EnglishGreeting(name);
16 break;
17 case Language.Chinese:
18 ChineseGreeting(name);
19 break;
20 }
21 }
22
23 public enum Language {
24 English,Chinese
25 }
26
27 public static void EnglishGreeting(string name)
28 {
29 Console.WriteLine("Morning," + name);
30 }
31
32 public static void ChineseGreeting(string name)
33 {
34 Console.WriteLine("早上好," + name);
35 }
36 }
2 {
3 static void Main(string[] args)
4 {
5
6 GreetPeople("Mingle",Language.English);
7 GreetPeople("明高",Language.Chinese);
8 Console.ReadLine();
9 }
10
11 public static void GreetPeople(string name,Language lang)
12 {
13 switch (lang) {
14 case Language.English:
15 EnglishGreeting(name);
16 break;
17 case Language.Chinese:
18 ChineseGreeting(name);
19 break;
20 }
21 }
22
23 public enum Language {
24 English,Chinese
25 }
26
27 public static void EnglishGreeting(string name)
28 {
29 Console.WriteLine("Morning," + name);
30 }
31
32 public static void ChineseGreeting(string name)
33 {
34 Console.WriteLine("早上好," + name);
35 }
36 }
如果在方法EnglishGreeting前不使用static,在调用的时候将报错:Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication4.Program.EnglishGreeting(string)'