La palabra clave `static`
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:
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
:
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
:
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:
index.cs
¿Todo estuvo claro?
Contenido del Curso
C# Beyond Basics
1. Estructuras Adicionales y Manejo de Archivos
2. Estructuras y Enumeradores
3. Introducción a la Programación Orientada a Objetos (POO)
C# Beyond Basics
La palabra clave `static`
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:
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
:
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
:
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:
index.cs
¿Todo estuvo claro?