Starting from this @Kartik Patel example , I have changed a little maybe now is more clear about static variable
public class Variable
{
public static string StaticName = "Sophia ";
public string nonStName = "Jenna ";
public void test()
{
StaticName = StaticName + " Lauren";
Console.WriteLine(" static ={0}",StaticName);
nonStName = nonStName + "Bean ";
Console.WriteLine(" NeStatic neSt={0}", nonStName);
}
}
class Program
{
static void Main(string[] args)
{
Variable var = new Variable();
var.test();
Variable var1 = new Variable();
var1.test();
Variable var2 = new Variable();
var2.test();
Console.ReadKey();
}
}
Output
static =Sophia Lauren
NeStatic neSt=Jenna Bean
static =Sophia Lauren Lauren
NeStatic neSt=Jenna Bean
static =Sophia Lauren Lauren Lauren
NeStatic neSt=Jenna Bean
Class Variable VS Instance Variable in C#
Static Class Members C# OR Class Variable
class A
{
// Class variable or " static member variable" are declared with
//the "static " keyword
public static int i=20;
public int j=10; //Instance variable
public static string s1="static class variable"; //Class variable
public string s2="instance variable"; // instance variable
}
class Program
{
static void Main(string[] args)
{
A obj1 = new A();
// obj1 instance variables
Console.WriteLine("obj1 instance variables ");
Console.WriteLine(A.i);
Console.WriteLine(obj1.j);
Console.WriteLine(obj1.s2);
Console.WriteLine(A.s1);
A obj2 = new A();
// obj2 instance variables
Console.WriteLine("obj2 instance variables ");
Console.WriteLine(A.i);
Console.WriteLine(obj2.j);
Console.WriteLine(obj2.s2);
Console.WriteLine(A.s1);
Console.ReadKey();
}
}
}
https://en.wikipedia.org/wiki/Class_variable
https://en.wikipedia.org/wiki/Instance_variable