C# Tutorial

C# Tutorial C# First Application C# Variables C# Data Types C# Operators C# Keywords

C# Control Statement

C# If Statements C# Switch Statements C# for Loop C# While Loop C# do While loop C# Jump Statements C# Function C# functions with out variable

C# Arrays

C# Arrays

C# Function

C# Function call by value C# Call by reference C# Passing array to function C# Multidimensional Arrays C# Jagged Arrays C# Params C# Array Class C# Command Line Arguments

C# Object Class

C# Object and Classes C# Constructors C# Destructor C# this Keyword C# static field C# static class C# Static Constructor C# Structs C# enum C# Properties

C# Inheritance

C# Inheritance C# Multilevel Inheritance C# Aggregation C# Member overloading C# Method Overriding C# Base

C# Polymorphism

C# Polymorphism C# Sealed

C# Abstraction

C# Abstraction C# Interface

C# Namespace

C# Namespace C# Access Modifiers C# Encapsulation

C# Strings

C# String

C# Misc

C# Design Patterns Dictionary in C# Boxing and Unboxing in C# Ref and Out in C# Serialization in C# The Foreach Loop in C# Exception in catch block C# C# Framework CLR in C# Garbage Collection in C# Collections in C# OOPS Concept in C# Palindrome in C# Abstract class Example in C# Linq Query in C# Sealed Class in C# What is the Net Framework in C# C# Hash Table With Examples Can We Create An Object For Abstract Class in C# Setting The Location Of The Label in C# Reverse Of String in C# Sorteddictionary Implementation in C# String And Stringbuilder in C# Virtual Keyword in C# Delegates And Events In C# C# Encription C# Error Logging Const and Readonly Variables in C# Difference Between Ref and Out in C# Singleton Class in C# Asqueryable in C# Filter Table in C# Operator Overloading in C# Difference between Delegates and Events in C# Finally Block in C# Reverse a String in C# Type Casting in C# What is Generics in C# Basic Database Operations Using C# C# Anonymous Methods C# Events C# Generics C# indexers C# Sortedlist With Examples C# Stack With Examples Hashset in C# With Examples List Implementation in C# Sortedset in C# With Examples C# Queue With Examples C# Multidimensional Indexers C# Multithreading C# New Features C# Overloading of Indexers C# Unsafe Code File Handling in C# C# in Depth Loggers in C# Nullable Types in C# How to Split String in C# Authentication and Authorization in C# Await in C# Binaryreader and Binarywriter in C# C# Attributes C# Delegates Directoryinfo Class in C# Export and Import Excel Data in C# File Class in C# Fileinfo Class in C# How to Cancel Parallel Operations in C# Maximum Degree of Parallelism in C# Parallel Foreach Loop in C# Parallel Invoke in C# Streamreader and Streamwriter in C# Textreader and Textwriter in C# JSON Serialization in C# Abstract Class in C# Application of Pointer in C# How to Convert String Data Type to Int in C# Readonly And Constant in C# Type Safe in C# Types of Variables in C# Use of Delegates in C# Atomic Methods Thread Safety and Race Conditions in C# Design Principles in C# Difference Between Abstraction and Encapsulation in C# Difference Between Struct and Class in C# Escape Sequence Characters in C# Parallel LINQ in C# What is IOC in C#

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