You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/c-sharp/concepts/classes/classes.md
+30-20Lines changed: 30 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -89,42 +89,52 @@ public class BankAccount {
89
89
90
90
## Static Classes
91
91
92
-
Static classes are defined using the `static` keyword and exclusively contain static members, such as methods, properties, and fields. Unlike regular classes, static classes cannot be instantiated with the `new` keyword. Instead, their members are accessed using the class name itself. These classes are commonly used for utility functions or to group related functionality.
93
-
The `static` keyword forces all fields, methods and properties to require the static keyword. This means that if you try to create any of these without the static keyword the code will not run and will produce a compile-time error. The reason behind this is that becuase you can not create an instance of a static class any methods, properties and fields that are not marked as static would be unreachable.
92
+
Static classes are defined using the `static` keyword and exclusively contain static members, such as methods, properties, and fields. Unlike regular classes, static classes cannot be instantiated with the `new` keyword. Instead, their members are accessed using the class name itself. These classes are commonly used for utility functions or to group related functionalities.
94
93
95
-
## Example
94
+
The `static` keyword forces all fields, methods, and properties to require this keyword. This means that if a user tries to create any of these without the `static` keyword, the code will not run and will produce a compile-time error. The reason behind this is that the user can not create an instance of a static class without using this keyword. Any methods, properties and fields that are not marked as `static` would be unreachable.
96
95
97
-
In the example below, a static class called `MyStaticClass` is created. The commented out code shows what you can not do with static classes. If these commented out pieces of code were to be uncommented the code would not compile. Take note on how we create properties and methods in the class and how we use it in the `CallingClass` class.
96
+
### Example
97
+
98
+
In the example below, a static class called `MyStaticClass` is created. The commented out code shows what a user can not do with static classes. If these commented out pieces of code were to be uncommented, the code would not compile. The example also shows how properties and methods in the `MyStaticClass` class are created and used in the `CallingClass` class:
98
99
99
100
```cs
100
101
usingSystem;
101
-
102
-
publicclassProgram
102
+
103
+
publicclassCallingClass
103
104
{
104
-
publicstaticvoidMain()
105
-
{
106
-
//The following will produce a compile-time error
107
-
//MyStaticClass NonStaticError = new MyStaticClass();
108
-
109
-
intDoubleFive=MyStaticClass.GetDouble(5);
110
-
Console.WriteLine(DoubleFive);
111
-
}
105
+
publicstaticvoidMain()
106
+
{
107
+
Console.WriteLine(MyStaticClass.StaticField);
108
+
109
+
// The following will produce a compile-time error
110
+
// MyStaticClass NonStaticError = new MyStaticClass();
111
+
112
+
intDoubleFive=MyStaticClass.GetDouble(5);
113
+
Console.WriteLine(DoubleFive);
114
+
}
112
115
}
113
116
114
117
publicstaticclassMyStaticClass
115
118
{
116
-
//The following will produce a compile-time error
117
-
//public int NonStaticField = 5;
119
+
//The following will produce a compile-time error
120
+
//public int NonStaticField = 5;
118
121
119
-
publicstaticintStaticField=10;
122
+
publicstaticintStaticField=20;
120
123
121
-
//Returns double of the value given
122
-
publicstaticintGetDouble(intvalue) {
124
+
//Returns double of the value given
125
+
publicstaticintGetDouble(intvalue) {
123
126
returnvalue*2;
124
-
}
127
+
}
125
128
}
126
129
```
127
130
131
+
The above code produces the following output:
132
+
133
+
```shell
134
+
20
135
+
10
136
+
```
137
+
128
138
## Partial classes
129
139
130
140
Partial classes in C# enable class definitions to be split across multiple files. Each part of the class is defined in a separate file and combined at compile time to create a single class. This is valuable for scenarios where a class becomes too large or complex, or when multiple developers need to work on different aspects of the class simultaneously.
0 commit comments