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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.