In C #, static means something that cannot be instantiated. You cannot create an object from a static class, nor can you access static members.
static class Class_Name
{
// static data members
// static method
}
There are two types of static members in the C# static class
Because the static class always contains static data members, the static data members are declared using a static keyword and are directly accessible using the class name. Memory for static data members is allocated individually without any relation to the object.
static class Class_name
{
public static nameofdatamember;
}
Since the static class always contains static methods, static methods are declared using a static keyword. Static methods only access to static data members, they cannot access non-static data members.
static class Class_name
{
public static nameofmethod()
{
// code
}
}
class Test
{
public static void Main()
{
Student st1 = new Student();
Student st2 = new Student();
st1.rollNumber = 3;
st2.rollNumber = 5;
Student.phoneNumber = 4929067;
Console.WriteLine(Student.phoneNumber);
}
}
class Student
{
public static int phoneNumber;
public static string name;
public int rollNumber;
static Student()
{
name = "unknown";
}
}
Stаtiс members аre ассessed with the nаme оf сlаss rаther thаn referenсe tо оbjeсts. Let's mаke оur Test сlаss соntаining Mаin methоd
4929067
Sоme рreсаutiоnаry роints in the end