×

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# 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# 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# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 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.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes 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# 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# 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# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

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# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 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# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 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.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.