--- marp: true paginate: true math: mathjax theme: buutti title: 10. Static Members, Methods and Classes --- # Static Members, Methods and Classes ## Static Members ### Non-static members * So far, we have used *__non-static__* fields in our classes * Meaning that each instance of the class holds its own version of the field, and changing the value of it only affects that instance: ```csharp class MyAwesomeClass { public int MyProperty { get; set; } } class Program { static void Main(string[] args) { MyAwesomeClass instance1 = new MyAwesomeClass(); MyAwesomeClass instance2 = new MyAwesomeClass(); instance1.MyProperty = 100; instance2.MyProperty = 200; // instance1.MyProperty is still 100 } } ``` --- * Likewise, non-static class methods **_have to_** be called through an instance: ```csharp class MyAwesomeClass { public void PrintText(string text) { Console.WriteLine(text); } } class Program { static void Main(string[] args) { MyAwesomeClass instance = new MyAwesomeClass(); instance.PrintText("Hello World"); // Outputs "Hello World" MyAwesomeClass.PrintText("Hello World"); // Results in an error } } ```
![w:500px](imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_1.png)
### Static members * Let's declare a property `MyProperty` with the `static` keyword * It can be referenced *through the class*, but ***not*** through the instance: ```csharp class MyAwesomeClass { public static int MyProperty { get; set; } = 100; } class Program { static void Main(string[] args) { MyAwesomeClass instance = new MyAwesomeClass(); Console.WriteLine(MyAwesomeClass.MyProperty); // Outputs "100" Console.WriteLine(instance.MyProperty); // Results in an error } } ```
![w:600px](imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_2.png)
### Static members: An example * In this example, a static field is used for keeping count on how many times the class has been instantiated: ```csharp class Person { public static int totalPersons = 0; private string name; public Person(string personName) // Person Constructor { name = personName; ++totalPersons; } public void PrintInfo() { Console.WriteLine("This person is called " + name + "."); Console.WriteLine("There are " + totalPersons + " persons total."); } } ``` --- * Now let's instantiate a couple of persons and print their info:
```csharp class Program { static void Main(string[] args) { Person steve = new Person("Steve"); Person wendy = new Person("Wendy"); steve.PrintInfo(); wendy.PrintInfo(); } } ```
![w:400px](imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_3.png)
## Static methods * Methods can also be static * What happens when you try to call a non-static method from a static method? ```csharp class Program { void PrintHelloName(string name) { Console.WriteLine("Hello, " + name); } static void Main(string[] args) { PrintHelloName(); // Will throw an error } } ```
![w:800px](imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_4.png)
## Static classes * Whole classes can also be static * Static classes cannot be instantiated, and all its members have to be static as well ```csharp static class Styling { public static string fontFamily = "Verdana"; public static float fontSize = 12.5f; } class Program { static void Main(string[] args) { Console.WriteLine ("Using font " + Styling.fontFamily + " " + Styling.fontSize + "px"); // Outputs "Using font Verdana 12.5px" Styling = new Styling(); // Results in an error } } ``` ## Exercise 1 * Create a class `Message` that has two `static` properties `int TotalMessages` and `string LastMessage`, and a *__non-static__* property `string MessageText`. * Add a constructor that takes a `string message` as a parameter, increases `TotalMessages` by one and sets the value of `LastMessage` to `message`. * Create a main loop that keeps asking the user for a new message. A new `Message` instance is then created with the user input message as an argument: ```csharp Message newMessage = new Message(message); ``` * `newMessage` is then added to a list `allMessages`. * Finally, the static values `Message.TotalMessages` and `Message.LastMessage` are printed.