1、C#的变量仅能从代码的本地作用域访问,访问该变量要通过这个作用域来实现
写个例子:
(1)访问不了的情况
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace bianliang
7 {
8 class Program
9 {
10
11 static void display()
12 {
13 Console.WriteLine("result:{0}",s);
14 }
15 static void Main(string[] args)
16 {
17 string s = "hello,welcome to fuzhou!";
18 display();
19 }
20 }
21 }
(2)可以访问
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace bianliang
7 {
8 class Program
9 {
10 static string s;
11 static void display()
12 {
13 // string s = "welcome to display!";
14 Console.WriteLine("result:{0}",Program.s);
15 }
16 static void Main(string[] args)
17 {
18 // string s;
19 s = "hello,welcome to fuzhou!";
20 display();
21 }
22 }
23 }