Fragment of static code


I am translating a Java code to C # and I found this code snippet:

    for (int i = 0; i < 64; i++)
        TABLE_T[i] = (int) (long) ((1L << 32) * Math.abs(Math.sin(i + 1)));

From what I read, java lets you define static snippets of code, but I can not find a way to pass this code to C #.

If someone could guide me, I would appreciate it too much.

asked by Alex Pérez 17.04.2018 в 18:21

2 answers


static { ... } is called inicializador estatico in java. It is the code that is executed only once in the whole program when a static member of the class is first accessed.

In it's called constructor estatico . This is done by creating a constructor with the class name but adding static to the subject and removing any visibility modifier:

public class MyClase{

    // constructor estatico. Se ejecuta una sola vez, al momento de acceder a una propiedad static
   static MyClase()
      // codigo
answered by 17.04.2018 / 18:30

I found this answer with some information that is the static block:


It is a constructor that will be executed with the definition of your static properties and will be executed even before creating the instance of your object (it affects the whole class, not only the object)

answered by 17.04.2018 в 18:29