Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
La palabra clave `static` | OOP Essentials
C# Beyond Basics

La palabra clave `static`La palabra clave `static`

An important keyword which you might have come across many times already is the `static` keyword. In the C# Basics Course, it was mentioned that the `static` is used when defining global variables - variables which are outside any specific method:
cs

index.cs

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

Del mismo modo, en otros métodos también, cuando estamos accediendo a campos, básicamente estamos accediendo a los campos de objetos que llaman a esos métodos, y no de la clase en sí, porque la clase normalmente no contiene ningún dato.

Sin embargo, hay una manera de almacenar datos en una clase directamente y hacer una propiedad accesible sin necesidad de crear un objeto. Podemos hacerlo simplemente haciendo que ese campo o método sea estático:

cs

index.cs

Dado que la ConsoleApp o la clase principal de cualquier programa que representa al propio programa no puede tener ningún objeto, necesitamos que sus métodos y campos sean estáticos. Por eso el método Main también es static:

cs

index.cs

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

1. What is the correct syntax for modifying the `value` field?
2. What is the correct syntax for modifying the `value` field?

What is the correct syntax for modifying the value field?

Selecciona la respuesta correcta

What is the correct syntax for modifying the value field?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 4. Capítulo 5
course content

Contenido del Curso

C# Beyond Basics

La palabra clave `static`La palabra clave `static`

An important keyword which you might have come across many times already is the `static` keyword. In the C# Basics Course, it was mentioned that the `static` is used when defining global variables - variables which are outside any specific method:
cs

index.cs

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

Del mismo modo, en otros métodos también, cuando estamos accediendo a campos, básicamente estamos accediendo a los campos de objetos que llaman a esos métodos, y no de la clase en sí, porque la clase normalmente no contiene ningún dato.

Sin embargo, hay una manera de almacenar datos en una clase directamente y hacer una propiedad accesible sin necesidad de crear un objeto. Podemos hacerlo simplemente haciendo que ese campo o método sea estático:

cs

index.cs

Dado que la ConsoleApp o la clase principal de cualquier programa que representa al propio programa no puede tener ningún objeto, necesitamos que sus métodos y campos sean estáticos. Por eso el método Main también es static:

cs

index.cs

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

1. What is the correct syntax for modifying the `value` field?
2. What is the correct syntax for modifying the `value` field?

What is the correct syntax for modifying the value field?

Selecciona la respuesta correcta

What is the correct syntax for modifying the value field?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 4. Capítulo 5
some-alt