Home > Backend Development > C#.Net Tutorial > volatile keyword in C#

volatile keyword in C#

PHPz
Release: 2023-08-26 19:05:05
forward
883 people have browsed it

C# 中的 volatile 关键字

To reduce concurrency issues in C#, use the volatile keyword. Let's look at an example.

Here's how to use volatile keyword for public variables -

class Program {
   public volatile int a;
   public void Program(int _a) {
      i = _i;
   }
}
Copy after login

Let's see another example: We have two static variables. Set them as new methods -

_out = "Welcome!";
_new = true;
Copy after login

We declared them as static before using volatile -

static string _out;
static volatile bool new;
Copy after login

Now you need to run the method on a thread -

new Thread(new ThreadStart(volatileFunc)).Start();
Copy after login

Read Get the value of a volatile variable-

if (_new) {
   Console.WriteLine(_out);
}
Copy after login

The above is the detailed content of volatile keyword in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template