| | |
Must value be determined at compile-time?
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
hi
Just a few days ago I've started learning C#.
BTW - I hope I'm not breaking any forum rules by putting three questions inside a single thread, but I don't want to "spam" this forum by making too many threads.
1)
From the book:
But if initialization value must indeed be determined at compile time, then the following code would produce an error, due to the fact that here the initialization value can’t be known at compile time, since memory on the heap is only allocated at run-time:
2)
The following quote talks about about the usefulness of auto-implemented properties:
Say I created class X, the member of which is also a public instance field named A, and put this class into assembly. Now some third party writes a code that access this field A. Later I decide to replace public field A with public property called A ( of same type as field was ), so I rewrite and recompile my class.
Now client would still access this property using the same code as it used for accessing field A -->
And since the client code remains unchanged, I don't see the need for recompiling it?
3) Why must the value of constant be determined at compile time rather than at run time, while read only field can be determined at run time?
Just a few days ago I've started learning C#.
BTW - I hope I'm not breaking any forum rules by putting three questions inside a single thread, but I don't want to "spam" this forum by making too many threads.
1)
From the book:
•
•
•
•
A field initializer is part of the field declaration, and consists of an equals sign followed by an expression that evaluates to a value. The initialization value must be determinable at compile time.
C# Syntax (Toggle Plain Text)
class A { B b = new B(); // here value of b can't be known until run time }
2)
The following quote talks about about the usefulness of auto-implemented properties:
•
•
•
•
Besides being convenient, auto-implemented properties allow you to easily insert a property where you might be tempted to declare a public field.
You might, however, be tempted to release a version of the code with a public field, and then in a later release change the field to a property. However, the semantics of a compiled variable and a compiled property are different. If, in a later release, you were to switch from a field to a property, any assemblies accessing that field in the first release would have to be recompiled to use the property. If you use a property in the first place, the client doesn’t have to be recompiled.
Now client would still access this property using the same code as it used for accessing field A -->
C# Syntax (Toggle Plain Text)
void Main { ... X x = new X(); int i = x.A; ... }
And since the client code remains unchanged, I don't see the need for recompiling it?
3) Why must the value of constant be determined at compile time rather than at run time, while read only field can be determined at run time?
Last edited by srubys; Jan 5th, 2009 at 1:46 pm.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
1)
Does have a value - as in effect it is a pointer to a B shaped peice of memory. Ergo, it has value, it is B shaped.
2) In your code if you have
And you change it to
You've changed where in memory its pointing to.. so it would need recompliation.
3) because, constants are hard coded into the exe, eg if you put in A= 50; rather than it taking A and placing 50 in it it, all instances of A are pointing to a reference of 50 within the exe (ish) - give or take a bit.
C# Syntax (Toggle Plain Text)
B b = new B();
Does have a value - as in effect it is a pointer to a B shaped peice of memory. Ergo, it has value, it is B shaped.
2) In your code if you have
C# Syntax (Toggle Plain Text)
Class X { public int A; }
And you change it to
C# Syntax (Toggle Plain Text)
class X { private int a; public int A { get { return a; } set { a=value; } }
You've changed where in memory its pointing to.. so it would need recompliation.
3) because, constants are hard coded into the exe, eg if you put in A= 50; rather than it taking A and placing 50 in it it, all instances of A are pointing to a reference of 50 within the exe (ish) - give or take a bit.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
•
•
B b = new B();
Last edited by ddanbe; Jan 5th, 2009 at 2:51 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
hi
1)
So b field has a value even though it doesn’t actually point to a specific location ( at compile time )? But couldn't then same argument also applied to, say, integer field:
Here someValue will be initialized at run time, when instance method someMethod() is called.
But we could still argue that at compile time someValue is a holder of a integer shaped piece of memory and thus someValue has a value, it is int shaped?!
2)
I realize that in the example you provided client code also needs to change ( since basically the interface changed ). But I don’t understand why client code should also change if it wants to access member A of the following class:
and we change class X to
Here client code should remain the same, since the interface didn't change!
3)
If I understood you correctly, then compiler doesn't place a constant A into an assembly, but rather just its value?
I don't quite understand what is gained by implementing constants in such way?
Also, why isn't static readonly field treated in the same manner?
BTW – is class ( not an instance of class ) created at compile time, or at runtime?
thank you
1)
•
•
•
•
C# Syntax (Toggle Plain Text)
B b = new B();
Does have a value - as in effect it is a pointer to a B shaped peice of memory. Ergo, it has value, it is B shaped.
C# Syntax (Toggle Plain Text)
public int someValue = new A().someMethod();
Here someValue will be initialized at run time, when instance method someMethod() is called.
But we could still argue that at compile time someValue is a holder of a integer shaped piece of memory and thus someValue has a value, it is int shaped?!
2)
•
•
•
•
2) In your code if you have
C# Syntax (Toggle Plain Text)
Class X { public int A; }
And you change it to
C# Syntax (Toggle Plain Text)
class X { private int a; public int A { get { return a; } set { a=value; } }
You've changed where in memory its pointing to.. so it would need recompliation.
C# Syntax (Toggle Plain Text)
class X { public int A }
and we change class X to
C# Syntax (Toggle Plain Text)
class X { public int A { get; set; } }
Here client code should remain the same, since the interface didn't change!
3)
•
•
•
•
3) because, constants are hard coded into the exe, eg if you put in A= 50; rather than it taking A and placing 50 in it it, all instances of A are pointing to a reference of 50 within the exe (ish) - give or take a bit.
I don't quite understand what is gained by implementing constants in such way?
Also, why isn't static readonly field treated in the same manner?
BTW – is class ( not an instance of class ) created at compile time, or at runtime?
thank you
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
•
•
•
•
1)
So b field has a value even though it doesn’t actually point to a specific location ( at compile time )? But couldn't then same argument also applied to, say, integer field:
•
•
•
•
C# Syntax (Toggle Plain Text)
public int someValue = new A().someMethod();
Here someValue will be initialized at run time, when instance method someMethod() is called.
But we could still argue that at compile time someValue is a holder of a integer shaped piece of memory and thus someValue has a value, it is int shaped?!
•
•
•
•
2)
I realize that in the example you provided client code also needs to change ( since basically the interface changed ). But I don’t understand why client code should also change if it wants to access member A of the following class:
C# Syntax (Toggle Plain Text)
class X { public int A }
and we change class X to
C# Syntax (Toggle Plain Text)
class X { public int A { get; set; } }
Here client code should remain the same, since the interface didn't change!
•
•
•
•
3)
If I understood you correctly, then compiler doesn't place a constant A into an assembly, but rather just its value?
I don't quite understand what is gained by implementing constants in such way?
What if then you had MAX_CONNECTIONS =30.. and you see "30" somewhere.. which is it?
•
•
•
•
Also, why isn't static readonly field treated in the same manner?
•
•
•
•
BTW – is class ( not an instance of class ) created at compile time, or at runtime?
thank you
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- C++ Performance Tips (C++)
- Need help with T/F and Multiple Choice (Java)
- Bound Checking (C++)
- Compare in templatized data structure (C++)
- path on the command-line (C++)
- Word Descrambler.... (C++)
- user given array size (C++)
- Super-power compare method? <?> (Java)
- Run-time Error when printing Array Contents. (C)
Other Threads in the C# Forum
- Previous Thread: Change fileNames using C# code
- Next Thread: update access database
| Thread Tools | Search this Thread |
.net 2007 access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mysql networking opening operator parse path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






