Object Oriented Programming

Class Variables in C#

Class variables are tied to the class itself, and its value is shared across all class objects.


Syntax
///within a class
modifier static variableName;

///accessing the class variable
className.variableName;

Notes

The static modifier prefix is what makes a variable a class variable.

Class variables can be accessed using the object, but this is discouraged.


Example
public class Car {
    public static int numberOfWheels = 4; ///all cars have 4 wheels
}

Console.WriteLine("Number of wheels: " + Car.numberOfWheels);

Add to Slack

< Access Modifiers   |   Standard Methods >

© 2018 SyntaxDB. All Rights Reserved.