When you use static
you define the variable, method or property at the level of the class and not the instance, that is, you can not apply concepts of programacion orientado a objetos
static (C # Reference)
If you want to create different objects of a class, do not use static, for example
public class Persona{
public string Nombre {get;set;}
}
when defining that class you could use new
, that is
Persona p1 = new Persona();
p1.Nombre = "Andres";
if you create a second instance
Persona p2 = new Persona();
p2.Nombre = "Susana";
will be independent of the first and will occupy a separate memory space.
With the same thing happens, you could define
public class Persona{
public string Nombre {get;set;}
public string Apellido {get;set;}
public string getFullName()
{
return $"{this.Apellido}, {this.Nombre}";
}
}
You'll see that by not using static I can apply this, which refers to the instance
Persona p1 = new Persona();
p1.Nombre = "Andres";
p1.Apellido = "Lopez";
textbox1.Text = p1.getFullName();
if you use static it would be
public class Persona
{
public string Nombre {get;set;}
public string Apellido {get;set;}
public static string getFullName(Persona p)
{
return $"{p.Apellido}, {p.Nombre}";
}
}
when using static you have to pass the instance by parameters
Persona p1 = new Persona();
p1.Nombre = "Andres";
p1.Apellido = "Lopez";
textbox1.Text = Persona.getFullName(p1);
to access getFullName
you use the name of the class, not the instance