static field initialization
-
class MyClass3
{
public static int i;} class Program { static void Main(string\[\] args) { Console.WriteLine(MyClass3.i); Console.ReadKey(); } }
I get a zero on the console. How is the static field getting initialized.
-
class MyClass3
{
public static int i;} class Program { static void Main(string\[\] args) { Console.WriteLine(MyClass3.i); Console.ReadKey(); } }
I get a zero on the console. How is the static field getting initialized.
simple
MyClass3.i = XX
XX any numeric value
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y<p?jxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can
-
class MyClass3
{
public static int i;} class Program { static void Main(string\[\] args) { Console.WriteLine(MyClass3.i); Console.ReadKey(); } }
I get a zero on the console. How is the static field getting initialized.
You can have a static constructor to initialize it or you can just assign it any value you wish from the code that uses this static class.
Giorgi Dalakishvili #region signature my articles #endregion
-
class MyClass3
{
public static int i;} class Program { static void Main(string\[\] args) { Console.WriteLine(MyClass3.i); Console.ReadKey(); } }
I get a zero on the console. How is the static field getting initialized.
If your question was, why the value is 0 although it has nowhere been initialized, then the C# language specification[^] tells you why: 5.2 Default values The following categories of variables are automatically initialized to their default values: * Static variables. * Instance variables of class instances. * Array elements. The default value of a variable depends on the type of the variable and is determined as follows: * For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (§4.1.2). * For a variable of a reference-type, the default value is null. Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference. regards
-
If your question was, why the value is 0 although it has nowhere been initialized, then the C# language specification[^] tells you why: 5.2 Default values The following categories of variables are automatically initialized to their default values: * Static variables. * Instance variables of class instances. * Array elements. The default value of a variable depends on the type of the variable and is determined as follows: * For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (§4.1.2). * For a variable of a reference-type, the default value is null. Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference. regards
Instance variables are initialized when we make an object of the class with the "new" keyword. Similarly with arrays. But in this case we dont use the new keyword. So how does it get initialized to its default value.
-
Instance variables are initialized when we make an object of the class with the "new" keyword. Similarly with arrays. But in this case we dont use the new keyword. So how does it get initialized to its default value.