![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
volatile keyword in C#
To reduce concurrency issues in C#, use the volatile keyword. Let us seen an example.
The following is how you use a volatile keyword for public variable −
class Program { public volatile int a; public void Program(int _a) { i = _i; } }
Let us see another example: We have two static variables. Set them in a new method −
_out = "Welcome!"; _new = true;
We declared them as static before using volatile −
static string _out; static volatile bool new;
Now you need to run the method on a thread −
new Thread(new ThreadStart(volatileFunc)).Start();
Read the value of the volatile variable −
if (_new) { Console.WriteLine(_out); }
Advertisements