static? What is it? and what is it for?

9

I have been programming desktop applications for a short time in C # , and I do not understand the difference between using static in my created variables or functions, that is:

private static String nombre_variable{get;set}
// cual es la diferencia entre
private String nombre_variable{get;set}

The same with the functions:

public static int sumar(int a, int b){...}
//cual es la diferencia entre
public int sumar(int a, int b){...}

I hope someone can enlighten me about this, besides telling me which one I should use.

    
asked by Shassain 03.04.2018 в 21:25
source

3 answers

7

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

    
answered by 03.04.2018 / 21:56
source
4
  

Translation of the original answer by Jon Skeet

static means effectively "associated with a type instead of any instance of the type". Then there is a set of static variables for a type (within an Application Domain) whether it has 0 instances or a million; you do not need an instance to access a static member, etc.

The exact point of initialization of the static variables depends on whether there is also a static constructor or not, but in general terms it is "once, usually before something significant occurs in the class". ( See this blog post to get a more detailed description).

While the readonly fields can be static or instance (that is, related to the type or related to an instance of the type), the const values are always implicitly static (they are compile time constants, so it would not make sense to have a copy for example).

Sometimes you can see static described as "shared among all instances of a type" - Personally I do not like that description, because it suggests that there must be at least one instance ... while in reality, you do not need No instance in order to use a static member. I prefer to think that they are completely separate, instead of being "shared" between instances.

  

My contribution : In summary you can not create an instance of a static class and it can only contain static members. Therefore, calls for a static class are like:

MiClaseStatic.MiMetodo(...) o MiClaseStatic.MiConstante.

An instance of a non-static class can be created and can contain non-static members (instance constructors, destructor, indexers). A non-static member of a non-static class can only be called through an object:

MiClaseNoStatic x = new MiClaseNoStatic (...);
x.MiClaseNoStaticMetodo(...);

Example:

class MiClaseEjemplo {
    public int MetodoInstancia() { return 1; }
    public static int MetodoStatic() { return 2; }
}

To call MetodoInstancia , you need an instance of the class:

MiClaseEjemplo instancia = new MiClaseEjemplo();
instancia.MetodoInstancia();   //Perfecto
instancia.MetodoStatic();     //No compilará

MiClaseEjemplo.MetodoInstancia();  //No compilará
MiClaseEjemplo.MetodoStatic();    //Perfecto
    
answered by 03.04.2018 в 21:43
0

Choosing between making a static method or not depends on several conditions.

A static method is also called a class method (otherwise it would be an instance method) that is, the logic does not depend on a particular instance, but the behavior is generic and only depends on the arguments it receives and on other static variables.

When to use static methods?

Use static methods to make clear that the method can be called in isolation. You also use static methods in patterns such as the Singleton .

When do I not use static methods?

By its nature one can not overwrite a static method, because it is just class. An interface can not have static method declarations, therefore if you use injection of dependencies you should not use static methods since you will not be able to implement Mocks on these methods.

    
answered by 03.04.2018 в 21:48