错误 10 非静态的字段、方法或属性“Test10.Program.a”要求对象引用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Test10
 7 {
 8     class Program
 9     {
10         //int a = 0;            注意存放位置!!!!!
11         const int i = 927;
12         static void Main(string[] args)
13         {
14            int a = 0;
15             try
16             {
17                 //i = 112;
18                 a = i / a;
19                 Console.WriteLine(a);
20             }
21             catch (Exception e)
22             {
23                 Console.WriteLine(e.Message);
24                 Console.ReadLine();
25             }
26             finally
27             {
28                 System.Console.WriteLine("无论是否发生异常,我都会显示。");
29             }  
30         }
31     }
32 }

在第10行那里因为自己开始定义了a,导致“非静态的字段、方法或属性“Test10.Program.a”要求对象引用”错误出现,将变量声明放到 Main方法里自然就好了。

另外,catch (Exception e),最后的e可以用任何ex等表示,只要ex.Message保持对应即可。

刚开始接触,可能感觉啥都新鲜,还请诸位知道,谢谢!

实事求是,不自以为是
原文地址:https://www.cnblogs.com/liuyaozhi/p/4854387.html