Sponsored Ad

Tuesday, May 18, 2010

What is use of Volatile Variable in C#

Volatile variable is used to declare a variable whose value can be change any time by any program.

 

Volatile variable is basically use with thread where number of thread access the same variable and the current thread can access the latest value written by another thread without use of lock.

Here is code to show how to declare volatile variable.

 

Source Code of Volatile variable declaration:

 

using System;
using System.Text;

namespace Console_App
{
    public class Class1
    {
        public volatile int i;

        public void fun_volatile(int j)
        {
            i = j;  
        }    
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 obj = new Class1();
            obj.fun_volatile(1);
            Console.ReadLine();
        }
    }

}

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers