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.
4.7 KiB
4.7 KiB
marp | paginate | math | theme | title |
---|---|---|---|---|
true | true | mathjax | buutti | 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:
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:
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 } }
Static members
- Let's declare a property
MyProperty
with thestatic
keyword - It can be referenced through the class, but not through the instance:
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 } }
Static members: An example
- In this example, a static field is used for keeping count on how many times the class has been instantiated:
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:
class Program
{
static void Main(string[] args)
{
Person steve = new Person("Steve");
Person wendy = new Person("Wendy");
steve.PrintInfo();
wendy.PrintInfo();
}
}
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?
class Program { void PrintHelloName(string name) { Console.WriteLine("Hello, " + name); } static void Main(string[] args) { PrintHelloName(); // Will throw an error } }
Static classes
- Whole classes can also be static
- Static classes cannot be instantiated, and all its members have to be static as well
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 twostatic
propertiesint TotalMessages
andstring LastMessage
, and a non-static propertystring MessageText
. - Add a constructor that takes a
string message
as a parameter, increasesTotalMessages
by one and sets the value ofLastMessage
tomessage
. - 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:Message newMessage = new Message(message);
newMessage
is then added to a listallMessages
.- Finally, the static values
Message.TotalMessages
andMessage.LastMessage
are printed.