Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Static


Share

Static Members

You have already used methods like Math.Max() and Math.Sqrt(). You call them on the class name (Math), without creating an object first:

Example

Console.WriteLine(Math.Max(5, 10));  // 10
Console.WriteLine(Math.Sqrt(64));    // 8

Try it Yourself »

This works because these methods are static. A static member belongs to the class itself, not to an object of the class.

The same is true for Console.WriteLine(), and for the static methods you created in the Methods chapter.


Static Fields

A normal field belongs to each object. In the example below, every Car object has its own model.

A static field belongs to the class. There is only one copy of it, and all objects share it. This is useful for counting how many cars were created:

Example

class Car
{
  public string model;
  public static int count = 0;

  public Car(string model)
  {
    this.model = model;
    count++;
  }
}

class Program
{
  static void Main(string[] args)
  {
    Car car1 = new Car("Volvo");
    Car car2 = new Car("BMW");
    Car car3 = new Car("Ford");
    Console.WriteLine(car1.model + ", " + car2.model + ", " + car3.model);
    Console.WriteLine("Cars created: " + Car.count);
  }
}

Try it Yourself »

Each car has its own model, but all cars share the same count. The constructor adds 1 to count each time a new car is created.

To use a static field, write the class name: Car.count, not car1.count.



Static Methods

A static method also belongs to the class. You call it with the class name, without creating an object, just like Math.Max():

Example

class Calculator
{
  public static int Add(int a, int b)
  {
    return a + b;
  }
}

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine(Calculator.Add(5, 3));  // 8
  }
}

Try it Yourself »

Inside its own class, you can call a static method without the class name. That is what you did in the Methods chapter, where MyMethod() and Main() were both static methods in the Program class.

A static method cannot use normal (non-static) fields. It does not belong to any object, so it does not know which car's model to use:

Example

class Car
{
  public string model = "Volvo";

  public static void ShowModel()
  {
    Console.WriteLine(model);  // Error
  }
}

class Program
{
  static void Main(string[] args)
  {
    Car.ShowModel();
  }
}

The compiler shows an error like this:

prog.cs(11,25): error CS0120: An object reference is required to access non-static member `MyApplication.Car.model'

Try it Yourself »

To use model, remove static from the method, and call it on an object, like myCar.ShowModel().


Static Classes

A whole class can also be static, like Math. A static class can only have static members, and you cannot create objects from it: new Math() gives an error.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.