C#作为全局的变量怎么实现?
C# 作为全局的变量怎么实现?
在C/C++中,全局变量是⼀种可以在程序任何位置上都可以使⽤的变量,那么C#中有没有类似的全局变量呢?答案上否定的,因为在C#中不存在全局变量这个概念。但是我们可以借签C#中的另外⼀种技术实现类似于全局变量的概念,既静态类。通过定义静态类的静态字段来记录保存全局状态。
using System;
using System.Threading;
public sealed class App
{
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
// Define a random number generator for testing.
static Random r = new Random();
static void Main()
{
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
// Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]); ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]); WaitHandle.WaitAll(waitHandles);
// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);
// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete."); ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]); ThreadPool.Que
ueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]); int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}
static void DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent)state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}
// This code produces output similar to the following:
//
// Main thread is waiting for BOTH tasks to complete.
// Performing a task for 7000 milliseconds.
// Performing a task for 4000 milliseconds.
// Both tasks are completed (time waited=7064.8052)
//
writeline教程// The main thread is waiting for either task to complete.
/
/ Performing a task for 2000 milliseconds.
// Performing a task for 2000 milliseconds.
// Task 1 finished first (time waited=2000.6528).
我个⼈探索如下:
1,使⽤类的静态成员来管理全局变量;
如下例⼦,在⼯程⾥⾯建⽴如下⼏个类,然后就可以调⽤。
class gInt //这个定义全局的 int 变量
{
public static int highScore;
public static int maxLevel;
}
class gStr //这个定义全局的 string 变量
{
public static string userName;
public static string userPassword;
}
class gCon //这⾥定义全局常量
{
public const int CharA = 65;
public const int CharB = 66;
public const string MyMotherland = "中国";
public const string MyCapital = "北京";
}
然后在⼯程的其他类的⽅法⾥⾯就可以做如下调⽤:
//测试全局变量的⽤法;
gStr.userName = "Jack.How"; //全局变量 userName string类型
gStr.userPassword = "Love1314"; //全局变量 userPassword string 类型 gInt.highScore = 9999; //全局变量 int 类型
gInt.maxLevel = 99; //全局变量 int 类型
// testTextBox.Text = gStr.userName + gStr.userPassword;
int sum1, sum2;
sum1 = gInt.highScore + gInt.maxLevel;
sum2 = gCon.CharA + gCon.CharB;
string str1;
str1 = gCon.MyMotherland + gCon.MyCapital; //全局常量
这个调⽤是可⾏的。
2,使⽤静态类的静态成员来管理全局变量;
//建⽴静态类和静态成员
public static class Global
{
private static string mUserName;
public static string UserName { get => mUserName; set => mUserName = value; } }
///调⽤地⽅
//赋值
Global.UserName ="23234";
/
/访问
string name111 = Global.UserName;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论