Static variable inside class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Static variable inside class



I am much confused about static variable actually i am executing below program.


class ABC

public static int prop get; set;
const int i =5;
static int j;

public ABC()

prop = 8;
j = 9;
Console.WriteLine("Under ABC class's constructor.");


public int getValue()

j = 6;
prop = 89;
return j;



class Program

static void Main(string args)

ABC obj = new ABC();
Console.WriteLine(obj.getValue());
//Console.WriteLine(ABC.j);
Console.ReadLine();




And its executing without any compile or run time error.



I have following confusions.





Please explain what from the official documentation about the static keyword is not clear enough.
– Camilo Terevinto
Aug 12 at 13:18


static





Yes camilo, i read this documentation and my main concern is, if we can assign static variable/property from any where within the same class (like normal variable) then what is the use of static constructor? As i used to know from starting that, static constructor is used to initiaze/assign the values to static variables of the class.
– saur
Aug 13 at 3:48






Some members down voted my question, please read full question and then think whether i did any research or not?
– saur
Aug 13 at 3:54





1 Answer
1



can we assign static variable/property inside the non static
constructor?



Yes.



can we assign static variable/property inside the instance method
also?



Yes.



If we can do assignment in above two cases for static
variable/property then what is the use of static constructor?



A static constructor is also called a type initializer. It is responsible for initializing the type it is defined in. You may use it to perform calculations that can be done upfront and are the same for all instances of that type. You'll therefore save some execution time when creating an instance because the calculation has already been done by the type initializer. Note that the type initializer runs before the type is used the first time. So you cannot deterministically tell when it actually runs. You can also not catch exceptions thrown by it because you don't actually invoke the type initializer yourself. You therefore need to be careful not to put error prone operations inside type initializers (f.e. do not do IO operations inside them).



Finally what are the locations inside a class where we can
assign/initialize a static variable/property?



From anywhere. Note that, even across threads, you can read and write to a static member from anywhere. This makes it very hard to find bugs that may occure due to a programm mutation a static somewhere in memory.



As a side note: Try to avoid having mutable static memory to keep your application simpler. If you really need to ... you should consider locking access to the static resource to prevent data races.


lock






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS