Skip to content

Commit a270ac5

Browse files
committed
Minor changes
1 parent cafba56 commit a270ac5

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

content/c-sharp/concepts/classes/classes.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,42 +89,52 @@ public class BankAccount {
8989

9090
## Static Classes
9191

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.
9493

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.
9695

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:
9899

99100
```cs
100101
using System;
101-
102-
public class Program
102+
103+
public class CallingClass
103104
{
104-
public static void Main()
105-
{
106-
//The following will produce a compile-time error
107-
//MyStaticClass NonStaticError = new MyStaticClass();
108-
109-
int DoubleFive = MyStaticClass.GetDouble(5);
110-
Console.WriteLine(DoubleFive);
111-
}
105+
public static void Main()
106+
{
107+
Console.WriteLine(MyStaticClass.StaticField);
108+
109+
// The following will produce a compile-time error
110+
// MyStaticClass NonStaticError = new MyStaticClass();
111+
112+
int DoubleFive = MyStaticClass.GetDouble(5);
113+
Console.WriteLine(DoubleFive);
114+
}
112115
}
113116

114117
public static class MyStaticClass
115118
{
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;
118121
119-
public static int StaticField = 10;
122+
public static int StaticField = 20;
120123

121-
//Returns double of the value given
122-
public static int GetDouble(int value) {
124+
// Returns double of the value given
125+
public static int GetDouble(int value) {
123126
return value * 2;
124-
}
127+
}
125128
}
126129
```
127130

131+
The above code produces the following output:
132+
133+
```shell
134+
20
135+
10
136+
```
137+
128138
## Partial classes
129139

130140
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

Comments
 (0)