using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static string myString; static void Write() { string myString = "String defined in Write()"; Console.WriteLine("Now in Write()"); Console.WriteLine("Local myString ={0}",myString); Console.WriteLine("Global myString ={0}", Program.myString); } static void Main(string[] args) { string myString = "String defined in Main()"; Program.myString = "global string"; Write(); Console.WriteLine("Now in Main()"); Console.WriteLine("Local myString={0}",myString); Console.WriteLine("Global myString={0}", Program.myString); Console.ReadLine(); } } }
结果:
转载于:https://blog.51cto.com/57388/1650875