+ 3

Why we can't access static variable with object reference in c# ?

In Java we can also access static variable with object reference but why not in c# ?

26th Feb 2017, 3:38 PM
SUCHIT YADAV
SUCHIT YADAV - avatar
1 Odpowiedź
+ 2
This applies to most object oriented languages, it's because a static variable is supposed to be independent from an instance you make. For example, static int y = 0 int Test(int x){ num = x; y++; return num; } What this is doing is that no matter how many instances you great y will never be reset until you restart the program. If you were able to call this with an object your assuming it applies to that instance but that can't happen since it's independent from all objects. The same idea applies to making static methods, you create the method but you don't need an object to go with it.
26th Feb 2017, 7:41 PM
Leveluptelevision
Leveluptelevision - avatar