c# 变量作用域

常见的private,protected,public等不再总结了。这里主要指出块级别的作用域。如下代码里面的变量s6。

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharp
{
    
public class TestScope
    {
        
public TestScope()
        {

            
//sA 在TestScope()方法内部有效.
            string[] sA = new string[5] { "H""e""l""l""o" };
            
            
//块作用域:s6只在foreach循环内部有效。
            foreach (string s6 in sA)
            {
                Console.WriteLine(s6);
            }

            
//在这里不能引用s6
            
//Console.WriteLine(s6);

            
//同样不能重新定义s6
            
//string s6 = "";
        }
    }
}

posted @ 2009-01-12 20:44  无尽思绪  阅读(3099)  评论(0编辑  收藏  举报