静态字段被类的所有实例所共享,即此类的所有实例都访问同一内存地址。 所以该内存位置的值变更的话,这种变更对所有的实例都可见。

    class MyClass
{
int number = ;
static int staticNumber = ; public void SetValue(int value1, int value2)
{
this.number = value1;
staticNumber = value2;
} public string DisplayValues()
{
return string.Format("number:{0}, staticNumber:{1}", this.number, staticNumber);
}
}
    class Program
{
static void Main(string[] args)
{
MyClass class1 = new MyClass();
MyClass class2 = new MyClass(); class1.SetValue(, ); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); class2.SetValue(, ); Console.WriteLine("Values in class2 are: {0}", class2.DisplayValues()); // 再次显示 class1 实例,发现 staticNumber 的值是 30.
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues());
}
}

进一步,如果有多个线程同时访问静态字段,并对其赋值,那么会出现什么样的情况呢?

(由于进程是一组资源,而进程中的多个线程会共享进程中的资源。)

实际操作发现,对int 字段的访问非常快,不会出现资源抢夺问题。

   public class Worker
{
// Volatile is used as hint to the compiler that this data member will be accessed by multiple threads.
private volatile bool shouldStop; private MyClass class3 = new MyClass(); // This method will be called when the thread is started.
public void DoWork()
{
while (!shouldStop)
{
Console.WriteLine("Worker thread: working and setting values.");
class3.SetValue(, );
} Console.WriteLine("Worker thread: terminating gracefully...");
} public void RequestStop()
{
this.shouldStop = true;
}
}
static void Main(string[] args)
{
MyClass class1 = new MyClass();
MyClass class2 = new MyClass(); class1.SetValue(, ); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); class2.SetValue(, ); Console.WriteLine("Values in class2 are: {0}", class2.DisplayValues()); // 再次显示 class1 实例,发现 staticNumber 的值是 20.
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); // Create the thread object.
Worker worker1 = new Worker();
Thread workerThread = new Thread(worker1.DoWork); // Start the worker thread.
workerThread.Start(); // Loop until worker thread acivates.
while (!workerThread.IsAlive) ; // Put the main thread to sleep for a while to allow worker thread do some work.
Thread.Sleep(); // Set values by main thread.
class1.SetValue(, );
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); // Request the worker thread to stop.
worker1.RequestStop(); // Use the Join method to block the current thread until the object's thread terminates.
workerThread.Join();
Console.WriteLine("Main thread: Worker thread has terminated."); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues());
}

如果静态字段是一个非托管资源,会怎么样呢?

(C# 基础) 静态字段,静态类,静态方法。的更多相关文章

  1. 零基础学Java(12)静态字段与静态方法

    静态字段与静态方法   之前我们都定义的main方法都被标记了static修饰符,那到底是什么意思?下面我们来看看 静态字段   如果将一个字段定义为static,每个类只有一个这样的字段.而对于非静 ...

  2. C#学习笔记----静态字段和静态方法

    1.使用关键字 static 修饰的字段或方法成为静态字段和静态方法,如 public static int num = 1;2.静态字段属于类,并为类所用.而非静态字段属于对象,只能被特定的对象专有 ...

  3. (10)C#静态方法,静态字段,静态类,匿名类

    6.静态方法 使用静态方法就可不必用类的实例化调用次函数 class Test { public static void method() { ........ } //当调用一个method()时就 ...

  4. Python学习:16.Python面对对象(三、反射,构造方法,静态字段,静态方法)

    一.构造方法 在使用类创建对象的时候(就是类后面加括号)就自动执行__init__方法. class A: def __init__(self): print('A') class B: def __ ...

  5. 深入理解 静态类和静态字段(C# 基础)

    序言 以前,总是被提醒,在编程过程中尽量少用静态变量,数据丢失什么的,今天有空,禁不住对静态变量的强烈好奇,跟我一起了解下静态家族的内幕吧. 静态类 定义 静态类与非静态类的重要区别在于静态类不能实例 ...

  6. JAVA的静态变量、静态方法、静态类

    静态变量和静态方法都属于静态对象,它与非静态对象的差别需要做个说明. (1)Java静态对象和非静态对象有什么区别? 比对如下: 静态对象                                ...

  7. 关于Java的静态:静态类、静态方法、静态变量、静态块等

    原文地址:Java static keyword - Class, Method, Variable, Block, import - JournalDev 很少看到文章能把静态这个问题解释的很清楚, ...

  8. 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法

    self是什么,就是一个函数,就是一个形式参数 4.self就是调用当前方法的对象 静态字段,公有属性 静态字段使用场景,每个对象中保存相同的东西时,可以使用静态字段,公有属性 5.封装的理解 类中封 ...

  9. java静态方法和静态字段

    public class Dog{ public static void main(String[]args){ A a= new A(); a.add(); //java实例对象可以访问类的静态方法 ...

随机推荐

  1. Windows自动化---模拟鼠标键盘

    1.PyUserInput(不推荐) python2可以使用PyUserInput库:(不推荐) 支持最基础的鼠标,键盘操作,可以剪贴. 安装的时候:pip install PyUserInput 需 ...

  2. Git的安装使用

    1.什么是Git Git是一个自由和开源的分布式版本管理工具,用于有效.高速的处理任何或大或小的项目.最初由Linux Torvalds编写,用于帮助管理Linux内核开发而开发的一个开放源码的版本管 ...

  3. Python序列类型各自方法

    在Python输入dir(str).dir(list).dir(tuple)可查看各种序列类型的所有方法. 对于某个方法不懂怎么使用的情况,可以直接help(str.split)对某个方法进行查询. ...

  4. sublime 配置主题

    默认主题可能看不清楚: 安装 PackageResourceViewer 安装Soda 主题 setting中加入 "theme": "Soda Light 3.subl ...

  5. [Leetcode]013. Roman to Integer

    public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...

  6. lintcode - 房屋染色

    class Solution { public: /* * @param costs: n x 3 cost matrix * @return: An integer, the minimum cos ...

  7. hutool http+天气预报

    中国天气接口:http://www.weather.com.cn/data/sk/地址.html,只显示当天. sojson接口:http://t.weather.sojson.com/api/wea ...

  8. WindowsMTU修改

    MTU是英文Maximum Transmission Unit的缩写,意为"最大传输单位".也就是通过TCP/IP协议所传输的数据包最大有多少字节,对于网速有极大的影响, MTU并 ...

  9. Murano Weekly Meeting 2015.09.29

    Meeting time: 2015.September.29th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting s ...

  10. python的面向对象的特性(继承、封装、多态)

    创建自已对象就python非常核心的概念,事实上,python被称为面向对象语言,本章会介绍如何创建对象.以及面向对象的概念:继承.封装.多态. 多态: 可对不同类的对象使用同样的操作. 封装:对外部 ...