Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The `static` Keyword | OOP Essentials
C# Beyond Basics
course content

Зміст курсу

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

bookThe `static` Keyword

cs

index

copy
1234567891011
using System; class ConsoleApp { static int val = 10; static void Main() { Console.WriteLine($"The value is {val}"); } }

It was a convenient way to describe the static keyword at that time because it was expected that the reader might not have any knowledge of objects or classes at that point, however now if you look at the code, you might realize that val is basically a field of the ConsoleApp class. However, note that the ConsoleApp class is slightly different from the classes we normally create. It is because ConsoleApp represents the program itself, and there is no object of the ConsoleApp. We will get back to this part in a bit.

Since a class is only a blueprint, more like a hollow shell, it does not normally contain data by itself, instead we create objects using that blueprint and then store and access data from those objects - as explained before:

cs

index

copy
12345678910111213141516171819202122232425262728
using System; class Complex { public int real; public int img; public Complex(int real, int img) { this.real = real; this.img = img; } } class ConsoleApp { static int val = 10; static void Main() { Complex c = new Complex(5, 10); // We can use and update the field of 'c' which is an instance. Console.WriteLine(c.real); c.real = 7; Console.WriteLine(c.real); } }

Even inside a class, when we are accessing those fields, we are accessing them through an object. For-example in a constructor, we are basically using the data that was passed by the object which called the constructor method:

cs

index

copy
12345678
public Complex(int real, int img) { // 'int real' and 'int img' contain the data which was passed // 'this.real' refers to the 'real' field of the object which is calling this constructor // same is the case for 'this.img' this.real = real; this.img = img; }

Similarly, in other methods as well, when we are accessing fields, we are basically accessing the fields of objects which call those methods, and not of the class itself because class normally does not contain any data.

However, there is a way to store data into a class directly and make a property accessible without needing to create an object. We can do that by simply making that field or method static:

cs

index

copy
123456789101112131415161718192021222324252627282930313233343536373839
using System; class Complex { public int real; public int img; // A static field can contain data // It is set to private because we don't want it to be manually modifiable from outside // This will track the total number of 'Complex' objects created private static int numbers = 0; public Complex(int real, int img) { this.real = real; this.img = img; numbers += 1; } // A static method // A static method or field can be accessed using the 'ClassName.PropertyName' syntax (see below) public static int getTotalComplexNumbers() { return numbers; } } class ConsoleApp { static void Main() { Console.WriteLine(Complex.getTotalComplexNumbers()); // 0 new Complex(1, 2); Console.WriteLine(Complex.getTotalComplexNumbers()); // 1 new Complex(2, 3); Console.WriteLine(Complex.getTotalComplexNumbers()); // 2 } }

Since the ConsoleApp or the main class of any program which represents the program itself cannot have any objects, we need to make their methods and fields static. This is why the Main method is static as well:

cs

index

copy
1234
static void Main() { // code }
1. Can a class directly store data?
2. What is the correct syntax for modifying the `value` field?
Can a class directly store data?

Can a class directly store data?

Виберіть правильну відповідь

What is the correct syntax for modifying the `value` field?

What is the correct syntax for modifying the value field?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 5
some-alt