Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Edit] C# Classes: Static Definition #5460 #5462

Merged
merged 12 commits into from
Nov 21, 2024
40 changes: 39 additions & 1 deletion content/c-sharp/concepts/classes/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,45 @@ public class BankAccount {

## Static Classes

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

### Example
<<<<<<< HEAD
In the example below, a static class called `MyStaticClass` is created. The commented out code shows what you can't 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 fields and methods in the class and how we use our static class in other classes.
=======
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=======

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.
>>>>>>> ffb4726d (Example for static classes added)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>>>>>> ffb4726d (Example for static classes added)

Remove this line.


```cs
using System;

public class Main
{
public static void Main()
{
//The following will produce a compile time error
//MyStaticClass NonStaticError = new MyStaticClass():

//
int DoubleFive = MyStaticClass.GetDouble(5);
Console.WriteLine(DoubleFive);
}
}

public static class MyStaticClass
{
//The folowing will produce a compile time error
//public int NonStaticField = 5;

public static int StaticField = 10;

//Returns double of the value given
public static int GetDouble(int value) {
return value * 2;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using System;
public class Main
{
public static void Main()
{
//The following will produce a compile time error
//MyStaticClass NonStaticError = new MyStaticClass():
//
int DoubleFive = MyStaticClass.GetDouble(5);
Console.WriteLine(DoubleFive);
}
}
public static class MyStaticClass
{
//The folowing will produce a compile time error
//public int NonStaticField = 5;
public static int StaticField = 10;
//Returns double of the value given
public static int GetDouble(int value) {
return value * 2;
}
}
using System;
public class Program
{
public static void Main()
{
//The following will produce a compile-time error
//MyStaticClass NonStaticError = new MyStaticClass();
//
int DoubleFive = MyStaticClass.GetDouble(5);
Console.WriteLine(DoubleFive);
}
}
public static class MyStaticClass
{
//The following will produce a compile -time error
//public int NonStaticField = 5;
public static int StaticField = 10;
//Returns double of the value given
public static int GetDouble(int value) {
return value * 2;
}
}

```

## Partial classes

Expand Down
Loading