@@ -4,14 +4,14 @@ title: Using static keyword
44
55# Static classes
66
7- One use case for static classes is creating utility classes (or "helper class ")
7+ One use case for static classes is creating utility classes (or "helper classes ")
88that contain related and frequently-used methods; making those methods easily callable
9- anywhere in the program. Some examples of static classes in C\# are ` Math ` and ` Console ` class .
9+ anywhere in the program. Some examples of static classes in C\# are the ` Math ` and ` Console ` classes .
1010
11- Pay attention to how these classes are used:
11+ Pay attention to how these classes are used:
1212
13- - ` Console ` object is never instantiated before use
14- - ` WriteLine ` method is called referring to the _ name of the class_ (not object identifier)
13+ - A ` Console ` object is never instantiated before use
14+ - The ` WriteLine ` method is called referring to the _ name of the class_ (not an object identifier)
1515
1616```
1717using System;
@@ -30,13 +30,13 @@ using System;
3030
3131class Program {
3232 static void Main() {
33- Console test = new Console();
33+ Console test = new Console();
3434 }
3535}
3636```
3737
3838Indeed, it is _ not possible_ to instantiate an object when a class is declared ` static ` .
39- Further, if a class is declared static, all its members (attributes, methods, constructors, etc.) must also be declared ` static ` .
39+ Further, if a class is declared static, all its members (attributes, methods, constructors, etc.) must also be declared ` static ` .
4040
4141## Static Calculator
4242
@@ -45,41 +45,41 @@ In your IDE create a new project. Then add a new class file called Calculator.cs
4545In Calculator.cs:
4646
4747#. Declare a ` static ` class and name it ` Calculator ` .
48- #. Add 5 ` public ` methods to ` Calculator ` class. Each method takes 2 arguments ` x ` and ` y ` of type ` double ` :
49- #. ` Add ` method that returns result of ` x + y ` .
50- #. ` Subtract ` method that returns result of ` x - y ` .
51- #. ` Multiply ` method that returns result of ` x * y ` .
52- #. ` Divide ` method that returns result of ` x / y ` .
53- #. ` Modulo ` method that returns result of ` x % y ` .
48+ #. Add 5 ` public ` methods to the ` Calculator ` class. Each method takes 2 arguments ` x ` and ` y ` of type ` double ` :
49+ #. ` Add ` method that returns the result of ` x + y ` .
50+ #. ` Subtract ` method that returns the result of ` x - y ` .
51+ #. ` Multiply ` method that returns the result of ` x * y ` .
52+ #. ` Divide ` method that returns the result of ` x / y ` .
53+ #. ` Modulo ` method that returns the result of ` x % y ` .
5454
55- After implementing ` Calculator ` ,
55+ After implementing ` Calculator ` ,
5656
57- #. Open the file that contains program's ` Main ` method
57+ #. Open the file that contains the program's ` Main ` method
5858#. Paste the following code inside ` Main ` method:
5959
6060 ```
6161 double x = 10d, y = 2d;
62-
62+
6363 Console.WriteLine($"{x} + {y} = {Calculator.Add(x, y)}");
6464 Console.WriteLine($"{x} - {y} = {Calculator.Subtract(x, y)}");
6565 Console.WriteLine($"{x} * {y} = {Calculator.Multiply(x, y)}");
6666 Console.WriteLine($"{x} / {y} = {Calculator.Divide(x, y)}");
67- Console.WriteLine($"{x} % {y} = {Calculator.Modulo(x, y)}");
67+ Console.WriteLine($"{x} % {y} = {Calculator.Modulo(x, y)}");
6868 ```
69-
70- Again, notice how
71- - no instance of `Calculator` is created before use, and
69+
70+ Again, notice how
71+ - no instance of `Calculator` is created before use, and
7272 - each `Calculator` method is called referring to the _name of the class_.
7373
7474#. Execute the program
75- - If your implementation of ` Calculator ` class matches the instructions, you will see meaningful output after executing the program.
75+ - If your implementation of ` Calculator ` class matches the instructions, you will see meaningful output after executing the program.
7676 - Otherwise review the instructions again and retrace your implementation steps to resolve any issues.
7777
7878# Static members in a non-static class
7979
8080A non-static class can contain both static and non-static class members.
8181
82- Study the following program implementation but \* do not\* execute it.
82+ Study the following program implementation but \* do not\* execute it.
8383After reading through the implementation, answer the questions below.
8484
8585Student.cs
@@ -90,28 +90,28 @@ using System;
9090class Student {
9191
9292 private int id;
93- private string name;
93+ private string name;
9494 private static string universityName = "Augusta University";
9595 private static int studentCount;
9696
9797 public Student(int id, string name){
9898 this.id = id;
9999 this.name = name;
100100 studentCount++;
101- }
101+ }
102102
103103 public static void DisplayStudentCount(){
104104 // does this work? uncomment to check
105105 // Console.WriteLine(name);
106-
106+
107107 Console.WriteLine($"Number of students: {studentCount}");
108108 }
109109
110110 public override string ToString(){
111111 return $"id: {id}\n"+
112112 $"name: {name}\n"+
113113 $"university: {universityName}";
114- }
114+ }
115115}
116116```
117117
@@ -129,37 +129,37 @@ class Program {
129129
130130 Student.DisplayStudentCount(); // first time
131131
132- Student bob = new Student(1112, "Bob");
132+ Student bob = new Student(1112, "Bob");
133133 Console.WriteLine(bob);
134134
135135 Student.DisplayStudentCount(); // second time
136136 }
137137}
138138```
139139
140- #. How many non-static attributes does ` Student ` class have?
141- #. How many static attributes does ` Student ` class have?
142- #. How many non-static methods does ` Student ` class have?
143- #. How many static methods does ` Student ` class have?
140+ #. How many non-static attributes does the ` Student ` class have?
141+ #. How many static attributes does the ` Student ` class have?
142+ #. How many non-static methods does the ` Student ` class have?
143+ #. How many static methods does the ` Student ` class have?
144144
145145#. What is the output of each of the following lines in "Program.cs":
146146 #. ` Console.WriteLine(alice); `
147147 #. ` Student.DisplayStudentCount(); // first time `
148148 #. ` Console.WriteLine(bob); `
149149 #. ` Student.DisplayStudentCount(); // second time `
150150
151- #. if the ` studentCount ` attribute was \* not\* ` static ` , what would be the output of:
151+ #. If the ` studentCount ` attribute was \* not\* ` static ` , what would be the output of:
152152 #. ` Student.DisplayStudentCount(); // first time `
153- #. ` Student.DisplayStudentCount(); // second time `
154-
153+ #. ` Student.DisplayStudentCount(); // second time `
154+
155155#. When a class contains both static and non-static members, is it possible to refer to non-static members inside a static method?
156- For example, if we try to refer to ` name ` attribute inside ` DisplayStudentCount ` , will it work? Why or why not?
157-
156+ For example, if we try to refer to the ` name ` attribute inside ` DisplayStudentCount ` , will it work? Why or why not?
157+
158158Check your answers by creating a matching program in your IDE and executing it.
159159
160160To check the last question, in Student.cs, uncomment the following line and verify its behavior matches your answer:
161161
162162```
163163// Console.WriteLine(name);
164- ````
164+ ````
165165
0 commit comments