Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Private Constructors in C#
Next article icon

C# | Static Class

Last Updated : 20 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members and static methods. It is not allowed to create objects of the static class and since it does not allow to create objects it means it does not allow instance constructor. Static classes are sealed, means you cannot inherit a static class from another class.

Syntax: 

static class Class_Name
{
// static data members
// static method
}


In C#, the static class contains two types of static members as follows:

  • Static Data Members: As static class always contains static data members, so static data members are declared using static keyword and they are directly accessed by using the class name. The memory of static data members is allocating individually without any relation with the object.
    Syntax: 
static class Class_name 
{
public static nameofdatamember;
}
  • Static Methods: As static class always contains static methods, so static methods are declared using static keyword. These methods only access static data members, they can not access non-static data members. 
    Syntax: 
static class Class_name {
public static nameofmethod()
{
// code
}
}

Example 1: 

C#
// C# program to illustrate the  // concept of static class using System;  namespace ExampleOfStaticClass {  // Creating static class // Using static keyword static class Author {      // Static data members of Author     public static string A_name = "Ankita";     public static string L_name = "CSharp";     public static int T_no = 84;      // Static method of Author     public static void details()     {         Console.WriteLine("The details of Author is:");     } }  // Driver Class public class GFG {      // Main Method     static public void Main()     {          // Calling static method of Author         Author.details();          // Accessing the static data members of Author         Console.WriteLine("Author name : {0} ", Author.A_name);         Console.WriteLine("Language : {0} ", Author.L_name);         Console.WriteLine("Total number of articles : {0} ",                                                Author.T_no);     } } } 

Output
The details of Author is: Author name : Ankita  Language : CSharp  Total number of articles : 84

Example 2:

C#
// C# program to demonstrate  // the concept of static class using System;   // declaring a static class public static class GFG {          // declaring static Method     static void display()     {         Console.WriteLine("Static Method of class GFG");     }      }  // trying to inherit the class GFG // it will give error as static  // class can't be inherited  class GFG2 : GFG {          public static void Main(String[] args) {                       } } 

Compile Time Error:

prog.cs(20,7): error CS0709: `GFG2': Cannot derive from static class `GFG'

Explanation: In the above example, we have a static class named as Author by using static keyword. The Author class contains static data members named A_name, L_name, and T_no, and a static method named as details(). The method of a static class is simply called by using its class name like Author.details();. As we know that static class doesn't consist object so the data member of the Author class is accessed by its class name, like Author.A_name, Author.L_name, and Author.T_no.  

Difference between static and non-static class 

Static ClassNon-Static Class
Static class is defined using static keyword.Non-Static class is not defined by using static keyword.
In static class, you are not allowed to create objects.In non-static class, you are allowed to create objects using new keyword.
The data members of static class can be directly accessed by its class name.The data members of non-static class is not directly accessed by its class name.
Static class always contains static members.Non-static class may contain both static and non-static methods.
Static class does not contain an instance constructor.Non-static class contains an instance constructor.
Static class cannot inherit from another class.Non-static class can be inherited from another class.


 


Next Article
Private Constructors in C#
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-OOP

Similar Reads

  • Static keyword in C#
    static is a modifier in C# which is applicable for the following: ClassesVariablesMethodsConstructorIt is also applicable to properties, event, and operators. To create a static member(class, variable, methods, constructor), precede its declaration with the keyword static. When a member is declared
    4 min read
  • Static Local Function in C# 8.0
    Static local functions introduced in C# 8.0. It is a local function created using the static keyword. This is used to prevent variables to captured from the enclosing. If a method is marked as static then we cannot access variables from the enclosing method. It enhances the performance by reducing t
    3 min read
  • C# Class and Objects
    Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single uni
    5 min read
  • C# | Namespaces
    Namespaces are used to organize the classes. It helps to control the scope of methods and classes in larger .Net programming projects. In simpler words you can say that it provides a way to keep one set of names(like class names) different from other sets of names. The biggest advantage of using nam
    5 min read
  • Private Constructors in C#
    Prerequisite: Constructors in C# Private Constructor is a special instance constructor present in C# language. Basically, private constructors are used in class that contains only static members. The private constructor is always declared by using a private keyword. Key PointsIt is the implementatio
    3 min read
  • C# Interview Questions and Answers
    C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications. He
    15+ min read
  • C# Program to Demonstrate the Static Constructor in Structure
    A Structure is a well-defined data structure that can hold elements of multiple data types. It is similar to a class because both are user-defined data types and both contain a bunch of different data types. You can also use pre-defined data types. However, sometimes the user might be in need to def
    2 min read
  • C# Program to Implement the Same Method in Multiple Classes
    C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, Implement the same me
    3 min read
  • C# Hello World
    The Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following: A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Mai
    4 min read
  • C# | Difference between Static Constructors and Non-Static Constructors
    Prerequisite: Constructors in C# Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class. Below are the differences
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences