great article, please add a section talking about Get only properties and how they compare to these?
V
vitorrubio
0 points
5 years ago
there are another static readonly advantages: you cant declare a DateTime const, but you can declare a static readonly DateTime.
Besides, static readonly properties can be changed in static class constructors (on your assembly's boot up)
H
Hadi
0 points
7 years ago
Hi Matthew, thanks for explanation. But i am still confused with definition of Static. it said "A static member (variable, method, etc) belongs to the type of an object rather than to an instance of that type.", I am confused about "type of an object " and " an instance of that type".
if i see your sample, its like a static member belongs to a class itself and not belongs to object.
Static Method MyMethod() is belongs to class MyClass
Not belongs to object myClassObj
var myClassObj = new MyClass(); // myClassObj is an object = instance of class MyClass
var result = myClassObj.MyMethod(); //Will not compile
Can you help me explain about "type of an object " and " an instance of that type"?
Thanks before.
A
Avery Musbach
0 points
7 years ago
A static readonly variable can be written to by a static constructor. That is an important difference between a const variable and a static readonly variable. For example, the static constructor could detect the operating system version and store it into a static readonly variable.
G
guilhas
0 points
9 years ago
Also a static variable example.
class Worker {
public static int workersCount = 0;
public Worker() { workersCount ++; }
}
var worker1 = new Worker();
var worker2 = new Worker();
var worker3 = new Worker();
Console.WriteLine("There are " + Worker.workersCount + " workers.")
> There are 3 workers.
P
Pankaj Desai
0 points
7 years ago
how can you make use of Instance constructor to edit Static part in your above example? its wrong example provided by you.
R
Rajeev Dandu
0 points
7 years ago
Good explanation. Thanks!
F
franquieu
0 points
9 years ago
Nice!
D
Dnil Vincent
0 points
8 years ago
Roget is really happy now :D
A
Ammar The Trainer
0 points
7 years ago
Good explanation thanks.
U
Unbreakable
0 points
6 years ago
Awesome!
K
Keith Ealanta
0 points
7 years ago
Generally a good explanation, but it seems less than helpful to describe static in terms of a function when the rest are being described in terms of data.
I'd highlight the mutable nature of a static (and the one-and-only-one aspect) by something like the following...
A static member(etc) belongs to a type not an instance of a class, and so if we declare a static thus...
class x{
private static int y = 0;
public void sety(int newy){y=newy}
public int gety(return y}
}
...then if we create two objects as follows, setting y on each, we get the following effect...
a=new x();
b=new x();
a.gety(); //returns 0
b.gety(); //returns 0
a.sety(10);
a.gety(); //returns 10
b.gety(); //returns 10
... this is because there is only one variable y for the class x, not one per instance of the class.
This is very useful for creatings singletons (see https://en.wikipedia.org/wi...) but is a trap in many other cases.
Note, that despite being static, it is still mutable.
D
disqus_VlR7veeWGr
0 points
6 years ago
Kind of old, but justt read it.
I've found another difference, that might help you to decide what to use (depending on your needs)
1 - You cannot use static readonly properties in a "switch" statement.
2 - You cannot see how many references a "const" have.
Awesome post, I'd add configuration stuff just to add to consideration though, since it's .Net specific talk. Most stuff people might do a const, it should also be considered for being an item in app.config, web.config. In the last paragraph you'd add: 5. "If you want the value to be configurable by administrators without re-compiling and deploying the code again, use config."
A
Amit Jadhav
0 points
6 years ago
Hi, I have below scenario for Static and Const, if Const is by default static then why output is different in below scenario:
Scenario 1:
Defined const as below:
private const int x = y + 100;
private const int y = z - 10;
private const int z = x;
Compiler gave error "The evaluation of the constant value for 'AccessModifiers.Program.x' involves a circular definition"
Scenario 2:
Defined static as below and no error this time
private static int a = 100;
private static int b = c-10;
private static int c = a;
Could you please help me find the answer.
M
Morilli
0 points
6 years ago
um you might wanna slowly, carefully read your code
M
Moe Asaad
0 points
6 years ago
Great Explanation! sometime we forget the basics.. Thank you for a great refresher.
great article, please add a section talking about Get only properties and how they compare to these?
there are another static readonly advantages: you cant declare a DateTime const, but you can declare a static readonly DateTime.
Besides, static readonly properties can be changed in static class constructors (on your assembly's boot up)
Hi Matthew, thanks for explanation. But i am still confused with definition of Static. it said "A static member (variable, method, etc) belongs to the type of an object rather than to an instance of that type.", I am confused about "type of an object " and " an instance of that type".
if i see your sample, its like a static member belongs to a class itself and not belongs to object.
Static Method MyMethod() is belongs to class MyClass
Not belongs to object myClassObj
var myClassObj = new MyClass(); // myClassObj is an object = instance of class MyClass
var result = myClassObj.MyMethod(); //Will not compile
Can you help me explain about "type of an object " and " an instance of that type"?
Thanks before.
A static readonly variable can be written to by a static constructor. That is an important difference between a const variable and a static readonly variable. For example, the static constructor could detect the operating system version and store it into a static readonly variable.
Also a static variable example.
class Worker {
public static int workersCount = 0;
public Worker() { workersCount ++; }
}
var worker1 = new Worker();
var worker2 = new Worker();
var worker3 = new Worker();
Console.WriteLine("There are " + Worker.workersCount + " workers.")
> There are 3 workers.
how can you make use of Instance constructor to edit Static part in your above example? its wrong example provided by you.
Good explanation. Thanks!
Nice!
Roget is really happy now :D
Good explanation thanks.
Awesome!
Generally a good explanation, but it seems less than helpful to describe static in terms of a function when the rest are being described in terms of data.
I'd highlight the mutable nature of a static (and the one-and-only-one aspect) by something like the following...
A static member(etc) belongs to a type not an instance of a class, and so if we declare a static thus...
class x{
private static int y = 0;
public void sety(int newy){y=newy}
public int gety(return y}
}
...then if we create two objects as follows, setting y on each, we get the following effect...
a=new x();
b=new x();
a.gety(); //returns 0
b.gety(); //returns 0
a.sety(10);
a.gety(); //returns 10
b.gety(); //returns 10
... this is because there is only one variable y for the class x, not one per instance of the class.
This is very useful for creatings singletons (see https://en.wikipedia.org/wi...) but is a trap in many other cases.
Note, that despite being static, it is still mutable.
Kind of old, but justt read it.
I've found another difference, that might help you to decide what to use (depending on your needs)
1 - You cannot use static readonly properties in a "switch" statement.
2 - You cannot see how many references a "const" have.
My 2 cents :)
Awesome post, I'd add configuration stuff just to add to consideration though, since it's .Net specific talk. Most stuff people might do a const, it should also be considered for being an item in app.config, web.config. In the last paragraph you'd add: 5. "If you want the value to be configurable by administrators without re-compiling and deploying the code again, use config."
Hi, I have below scenario for Static and Const, if Const is by default static then why output is different in below scenario:
Scenario 1:
Defined const as below:
Compiler gave error "The evaluation of the constant value for 'AccessModifiers.Program.x' involves a circular definition"
Scenario 2:
Defined static as below and no error this time
private static int c = a;
Could you please help me find the answer.
um you might wanna slowly, carefully read your code
Great Explanation! sometime we forget the basics.. Thank you for a great refresher.
Thank you so much!
Redacted due to stupidity
Redacted due to stupidity
great post
Thank you!