• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一蓑烟雨
C/C++,Linux,语音技术
博客园    首页    新随笔    联系   管理    订阅  订阅
变量的作用域

1、C#的变量仅能从代码的本地作用域访问,访问该变量要通过这个作用域来实现

写个例子:

(1)访问不了的情况      

error
 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)可以访问

right
 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 }



posted on 2011-12-07 11:00  lovemu  阅读(206)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3