You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
4.7 KiB
Markdown
199 lines
4.7 KiB
Markdown
---
|
|
marp: true
|
|
paginate: true
|
|
math: mathjax
|
|
theme: buutti
|
|
title: 10. Static Members, Methods and Classes
|
|
---
|
|
|
|
# Static Members, Methods and Classes
|
|
|
|
<!-- headingDivider: 5 -->
|
|
<!-- class: invert -->
|
|
|
|
## 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
|
|
}
|
|
}
|
|
```
|
|
<div class='centered'>
|
|
|
|

|
|
|
|
</div>
|
|
|
|
### 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
|
|
}
|
|
}
|
|
```
|
|
|
|
<div class='centered'>
|
|
|
|

|
|
|
|
</div>
|
|
|
|
|
|
### 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:
|
|
|
|
<div class='columns21' markdown='1'>
|
|
<div markdown='1'>
|
|
|
|
```csharp
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Person steve = new Person("Steve");
|
|
Person wendy = new Person("Wendy");
|
|
steve.PrintInfo();
|
|
wendy.PrintInfo();
|
|
}
|
|
}
|
|
```
|
|
|
|
</div>
|
|
<div markdown='1'>
|
|
|
|

|
|
|
|
</div>
|
|
</div>
|
|
|
|
## Static methods
|
|
|
|
* Methods can also be static — for example, `Console.WriteLine`
|
|
* 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
|
|
}
|
|
}
|
|
```
|
|
|
|
<div class='centered'>
|
|
|
|

|
|
|
|
</div>
|
|
|
|
## 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
|
|
<!--_class: "exercise invert" -->
|
|
|
|
* 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.
|
|
|