This lab serves multiple goals:
- To teach you how a static class differs from a non-static one,
- To illustrate the usefulness of static classes,
- To teach you how a non-static class can manipulate static fields.
Static Classes — Warm-Up {#static-classes-warm-up}
One use case for static classes is creating utility classes (or “helper
classes”) that contain related and frequently-used methods Using a
static class makes those methods easily callable anywhere in the
program. Some examples of static classes in C# are the Math and
Console classes.
Pay attention to how these classes are used:
-
A
Consoleobject is never instantiated before use. -
The
WriteLinemethod is called referring to the name of the class (not an object identifier):Console.WriteLine("calling a static method");
| ❓ Question |
|---|
Using your IDE, check what happens if you do the following: Console test = new Console();. |
Solution:
Indeed, it is not possible to instantiate an object when a class is
declared static. Furthermore, if a class is declared static, all its
members (e.g., attributes, methods, constructors, etc.) must also be
declared static.
Static Calculator
In your IDE create a new project. Then add a new class file called Calculator.cs
In Calculator.cs:
- Declare a
staticclass and name itCalculator. - Add 5
publicmethods to theCalculatorclass. Each method takes 2 argumentsxandyof typedouble:Addmethod that returns the result ofx + y.Subtractmethod that returns the result ofx - y.Multiplymethod that returns the result ofx * y.Dividemethod that returns the result ofx / y.Modulomethod that returns the result ofx % y.
After implementing Calculator,
-
Open the file that contains the program’s
Mainmethod -
Paste the following code inside the
Mainmethod:double x = 10d, y = 2d; Console.WriteLine($"{x} + {y} = {Calculator.Add(x, y)}"); Console.WriteLine($"{x} - {y} = {Calculator.Subtract(x, y)}"); Console.WriteLine($"{x} * {y} = {Calculator.Multiply(x, y)}"); Console.WriteLine($"{x} / {y} = {Calculator.Divide(x, y)}"); Console.WriteLine($"{x} % {y} = {Calculator.Modulo(x, y)}");Again, notice how
- no instance of
Calculatoris created before use, and - each
Calculatormethod is called referring to the name of the class.
- no instance of
-
Execute the program
- If your implementation of the
Calculatorclass matches the instructions, you will see meaningful output after executing the program. - Otherwise, review the instructions again and retrace your implementation steps to resolve any issues.
- If your implementation of the
Static Members in a Non-static Class
A non-static class can contain both static and non-static class members.
Download, extract, and study this project implementation, but do not execute it. After reading through the implementation, answer the questions below.
-
How many non-static attributes does the
Studentclass have? -
How many static attributes does the
Studentclass have? -
How many non-static methods does the
Studentclass have? -
How many static methods does the
Studentclass have? -
What is the output of each of the following lines in “Program.cs”:
Console.WriteLine(alice);Student.DisplayStudentCount(); // first timeConsole.WriteLine(bob);Student.DisplayStudentCount(); // second time
-
If the
studentCountattribute was notstatic, what would be the output of:Student.DisplayStudentCount(); // first timeStudent.DisplayStudentCount(); // second time
-
When a class contains both static and non-static members, is it possible to refer to non-static members inside a static method? For example, if we try to refer to the
nameattribute insideDisplayStudentCount, will it work? Why or why not?
Check your answers by creating a matching program in your IDE and executing it.
To check the last question, in Student.cs, uncomment the following line and verify its behavior matches your answer:
// Console.WriteLine(name);