×

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only once to a static field at the time of class loading which is common among all the objects.
Advantages:
  1. We don't need to create object to access static data that's why it is memory efficient.
  2. The data which is common among all the objects is made static to avoid redundancy of data.
Static Filed: A filed that is declared static is called static filed. Static field gets memory only once at the time of class loading. The common property among all the objects is referred by static field such as company name in case of employees, college name in case of students.

C# Static Filed Example: Let's see a simple example of static field in C#.
using System;
public class Student
{
            int id;
            string course,name;
            static int batch=2017;
            public Student(int id,string name,string course)
            {
                        this.id=id;
                        this.name=name;
                        this.course=course;
            }
            public void show()
            {
                        Console.WriteLine(id+" "+name+" "+course+" "+batch);
            }
            public static void Main(string[] args)
            {
                        Student s1=new Student(101,"Ravi Dubey","M.C.A.");
                        Student s2=new Student(102,"Ravi Malik","B. Tech.");
                        s1.show();
                        s2.show();
            }
}
Output
101 Ravi Dubey M.C.A. 2017
102 Ravi Malik B. Tech. 2017
C# Static field Example: Counting Number of instances
using System;
public class Student
{
            int id;
            string course,name;
            static int batch=2017;
            static int count=0;
            public Student(int id,string name,string course)
            {
                        this.id=id;
                        this.name=name;
                        this.course=course;
                        count++;
            }
            public void show()
            {
                        Console.WriteLine(id+" "+name+" "+course+" "+batch);
            }
            public static void Main(string[] args)
            {
                        Student s1=new Student(101,"Ravi Dubey","M.C.A.");
                        Student s2=new Student(102,"Ravi Malik","B. Tech.");
                        Student s3=new Student(103,"Ravi Gautam","B.C.A.");
                        s1.show();
                        s2.show();
                        s3.show();
                        Console.WriteLine("Number of students are : "+count);
            }
}
Output
101 Ravi Dubey M.C.A. 2017
102 Ravi Malik B. Tech. 2017
103 Ravi Gautam B.C.A. 2017
Number of students are : 3

Related Topics

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.