Lecture overview -- Keyboard shortcut: 'u'  Previous page: Instance Methods -- Keyboard shortcut: 'p'  Next page: Class Methods -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 15 : 29
Object-oriented Programming in C#
Classes and Objects
Class Variables

Class variables are less important than instance variables when we do object-oriented programming. Class variables are similar to global variables if they belong to a public class.

A class variable belongs to the class, and it is shared among all instances of the class.

  • Class variables

    • are declared by use of the static modifier in C#

    • may be used as global variables - associated with a given class

    • do typically hold meta information about the class, such as the number of instances

/user/normark/oop-csharp-1/sources/c-sharp/bank-account/bank-account-2/bank-account.csThe class BankAccount with a class variable.

The class BankAccount with a class variable nextAccountNumber, which can be used as a unique account number when a BankAccount class is instantiated.

/user/normark/oop-csharp-1/sources/c-sharp/bank-account/bank-account-2/bank-account-client.csA client of class BankAccount.


/user/normark/oop-csharp-1/sources/c-sharp/bank-account/bank-account-2/outputOutput of the BankAccount client program.


Go to exerciseSharing the Random Generator
Read more about class variables in the text book version of this material.