What is a static in C#?


Static Keyword in C

Static è una keyword in C# che può essere utilizzata per rendere un member di un tipo specifico. A class, field, method, property, operator, event or constructor can use the static modifier. In genere, viene utilizzato quando la data e il comportamento di una classe non dipendono dall’identità dell’oggetto.

Vantaggi delle Classi Statiche

Il vantaggio dell’utilizzo di una classe statica è che il compiler può verificare che non vengano aggiunti inavvertitamente instance members. Il compilatore garantisce che instances di questa classe non possono essere create. Static classes non possono essere trasmesse perché sono sigillate.

Differenza tra Statico e Costante in C

Constants are assigned only to value types and are set at compile time itself. Per esempio, la statica variable è una proprietà della classe piuttosto che un’instance della classe. It is stored in the data segment area of memory, and all instances of that class receive the same value.

Differenze tra Classi Statiche e Singleton in C

A singleton consente a class for which there is only one persistent instance throughout the lifetime of an application. Invece, a static class consente solo static methods e non può essere passata come parameter. A Singleton è in grado di implementare interfaces, consentire l’inheritance e inheritare da altre classes.

Lascia un commento