C# Program using a static variable


In this program, You will learn how to implement static variables in C#.


int x = 10;

static int y = 10;

Example: How to implement static variable in C#.

using System;
public class Test
{
	int x = 10;
	static int y = 10;

	void change()
	{
		x++;
		y++;
	}

	void print()
	{
		Console.WriteLine("The value of x is:" + x);
		Console.WriteLine("The value of y is:" + y);
	}

	public static void Main(string[] args)
	{
		Test obj1 = new Test();
		obj1.change();
		obj1.print();

		Test obj2 = new Test();
        obj2.change();
        obj2.print();
	}
}

Output:

The value of x is:11
The value of y is:11
The value of x is:11
The value of y is:12