Conteúdo do Curso
C# Beyond Basics
C# Beyond Basics
The `static` Keyword
index
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:
index
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:
index
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
:
index
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:
index
static void Main() { // code }
Obrigado pelo seu feedback!