Advertisement
//C# Example of accessing a static variable/static method within the scope of its class.
using System;
class A
{
//A static variable of class A is initialized to 20
static int i = 20;
//A static method of class A
public static void Display()
{
Console.WriteLine("A");
}
public static void Main(String[] ar)
{
//Directly accessing a static variable
Console.WriteLine(i);
//Accessing a static variable by using its class name and a dot operator
Console.WriteLine(A.i);
//Directly accessing a static method
Display();
//Accessing a static method by using its class name and a dot operator
A.Display();
}
}
20
20
A
A
Advertisement
//C# Example of incorrect direct access of a static variable/static method outside its class.
using System;
class A
{
//A static variable of class A is initialized to 10
public static int i=10;
//A static method of class A
public static void Display()
{
Console.WriteLine("A");
}
}
class B
{
public static void Main(String[] ar)
{
//Directly accessing a static variable, outside the scope of its class
//Outside the scope of its class
Console.WriteLine(i);
//Directly accessing a static method, outside the scope of its class
Display();
}
}
A.cs(23,20): error CS0103: The name 'i' does not exist in the current context
A.cs(26,2): error CS0103: The name 'Display' does not exist in the current context
A.cs(8,12): warning CS0414: The field 'A.i' is assigned but its value is never used
//C# Example of correct access of a static variable/static method outside its class.
using System;
class A
{
//A static variable of class A is initialized to 10
public static int i=10;
//A static method of class A
public static void Display()
{
Console.WriteLine("A");
}
}
class B
{
public static void Main(String[] ar)
{
//Accessing the static variable outside its class,
//using its class name and a dot operator
Console.WriteLine(A.i);
//Accessing the static method outside its class,
//using its class name and a dot operator
A.Display();
}
}}
10
A
This program is the same as the last one but in this code, we have accessed the static variable, i, and the static method,
Display(), outside the scope of its class A, by using its class name and a dot operator, therefore,
no compile errors are issued.
//C# We cannot access an instance variable or instance methods from within a static method
using System;
class A
{
//A non-static i.e. instance variable of class A is initialized to 10
int i = 10;
//A non-static i.e instance method of class A
public void ShowA()
{
Console.WriteLine(i);
}
//A static method of class A
public static void Display()
{
//Accessing the instance variable from the static method, won't work
Console.WriteLine(i);
//Accessing the instance method from static method, won't work
ShowA();
}
public static void Main(String[] ar)
{
//Accessing a static method directly in its class is fine
Display();
}
}
A.cs(21,20): error CS0120: An object reference is required for the non-static field, method, or property 'A.i'
A.cs(22,2): error CS0120: An object reference is required for the non-static field, method, or property 'A.ShowA()'
//C# Default values of static variables
using System;
class A
{
//Uninitialized static variables
static int i;
static float f;
static String s;
static bool b;
public static void Main(String[] ar)
{
//Printing the values of uninitialized static variables
Console.WriteLine(i);
Console.WriteLine(f);
Console.WriteLine(b);
if(s == null) //A static object of any class is always initialized to null by default
Console.WriteLine("null");
}
}
0
0
False
null
Static Type | Default Value |
---|---|
int, short, byte, long, float, double | 0 |
bool | False |
Object | null |
//C# The value of a static variable is same and shared by all objects of its class.
using System;
class A
{
//A static variable of class A
static int value = 10;
//An instance method
public void SetValue(int v)
{
value = v;
}
//An instance method
public int GetValue()
{
return value;
}
public static void Main(String[] ar)
{
//Creating first object of class A
A ob1 = new A();
//Setting the value of static variable
//by using the first object
ob1.SetValue(20);
//Creating second object of class A
A ob2 = new A();
//Setting the value of static variable
//by using the second object
ob2.SetValue(30);
//Accessing value of static variable
//by using the first object
Console.WriteLine(ob1.GetValue());
//Accessing value of static variable
//by using the second object
Console.WriteLine(ob2.GetValue());
}
}
30
30
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement