Why C# will allow this :
public class MyClass { static int A=1; static int B=A+1; }
But won't allow ("A field initializer cannot reference the non-static field, method, or property") this
public class MyClass { int A=1; int B=A+1; }
I thought that it's the order that is guaranteed (with static fields) to be sequential initialized as it appears , but it's also applied here as you can see :
public class MyClass { int A=((Func<int>)(delegate(){ Console.WriteLine ("A"); return 1;}))(); int B=((Func<int>)(delegate(){ Console.WriteLine ("B"); return 2;}))(); int C=((Func<int>)(delegate(){ Console.WriteLine ("C"); return 3;}))(); } void Main() { var a = new MyClass(); }
Result :
A B C
Question
I'm more interested with the reason/logic for why it was restricted. just for curiosity.
nb didn't find any duplicate.
It is fast The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.
It helps you understand how a computer works By learning C, you can be able to understand and visualize the inner workings of computer systems. This can include aspects like allocation and memory management along with their architecture and the overall concepts that drive programming.
The biggest advantage of using C is that it forms the basis for all other programming languages. The mid-level language provides the building blocks of Python, Java, and C++. It's a fundamental programming language that will make it easier for you to learn all other programming languages.
Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.
I'm more interested with the reason/logic for why it was restricted. just for curiosity.
If you read the C# Language Spec, 10.11.3, it hints as to the rationale here. In discussing variable initializers:
It is useful to think of instance variable initializers and constructor initializers as statements that are automatically inserted before the constructor-body.
Since these are "inserted before the constructor", they are being executed prior to this
being valid, so allowing you to refer to other members (effectively this
) would be problematic.
Note that this is consistent with how static
fields work, as well. In both cases, you are allowed to access static data, but not instance data. The error message you receive ("A field initializer cannot reference the non-static field, method, or property") directly notes this.
"Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object." -static MSDN
When A
and B
are declared static they belong to the type MyClass
and all instances of MyClass
will have the same value for A
and B
. The static constructor will run before the class is instantiated but after the program has started. At that point, A
is already defined, and thus B
can reference it.
On the other hand, when A
and B
are not static, they only belong to the instance of MyClass
. While compiling, the field B
will attempt to be initialized based on a value from A
which has not yet been initialized. There is no instance of MyClass
while the compiler is running and therefore there is no value of A
to reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With