472,126 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

constant vs. read only property

Emm, being a newbie to C#, can someone explain to me what is the difference
between a constant (as a Class member) and a read only property??

Also I simply cannot grasp the concept of static methods, can someone please
explain it to me in human language??
Nov 16 '05 #1
8 19070
Davmp ^srisson wrote:
Emm, being a newbie to C#, can someone explain to me what is the
difference between a constant (as a Class member) and a read only
property??

Also I simply cannot grasp the concept of static methods, can someone
please explain it to me in human language??


const means it's not going to change, read-only it might chane but
_you_ cannot change it.

static method is a method which doesn't require/care for the actual
object. It's pretty much like a global function - you might think of it
as the global function but in your class namespace.
Nov 16 '05 #2
"Jack Hanebach" <ja*****@spam.hanebach.net> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
const means it's not going to change, read-only it might chane but
_you_ cannot change it.


You being the person using the class, if you are the person creating the
class then you can change it. :-)

Michael Culley
Nov 16 '05 #3
Michael Culley wrote:
"Jack Hanebach" <ja*****@spam.hanebach.net> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
const means it's not going to change, read-only it might chane but
_you_ cannot change it.

You being the person using the class, if you are the person creating the
class then you can change it. :-)

An object can change the value of its own readonly properties but
anything using that object cannot.
JB
Nov 16 '05 #4
'readonly' variables can only be assigned a value in the declaration
or in the variable's class constructor.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #5
Emmm... First there is a difference between const and readonly and it
has to do with versioning or whether the value is updated at runtime or
only if you recompile the _caller_.
http://www.geocities.com/jeff_louie/OOP/oop5.htm

6) Const is Hard Coded Into the Caller and not Version-able
I find this one a bit weird. A const field value is hard coded into the
caller for optimization so that the value is not updated until you
recompile the caller. If you want to version-able read only field
declare it readonly. Finally, you can provide a get only Property like
this:

public string ModelName
{
get
{
return modelName; // danger, return ModelName --> stack
overflow!
}
}

As for static.
http://www.geocities.com/jeff_louie/OOP/oop4.htm

What Is a Static Field or Method?
Let's change the question. When is a field or method not part of an
object? Answer: when it is part of the class! Remember, an object is an
instance of a class and each object exists in a separate space in
memory. It is possible to access class fields and class methods without
creating an instance of a class using the "static" key word. Declaring a
field or method with the static key word, tells the compiler that the
field or method is associated with the class itself, not with instances
of the class. In a sense, static or "class" fields and methods are
global variables and methods that you can touch using the class name. If
you think of a class as a blueprint used to create objects, then you can
think of static fields and methods are being part of the blueprint
itself. There is only one copy of the static fields and methods in
memory, shared by all instances of the class.

Hope that helps,
Jeff
Emm, being a newbie to C#, can someone explain to me what is the

difference between a constant (as a Class member) and a read only
property??<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Hi,
Actually readonly values can be set only in the constructor of the type
decalring the readonly field. Once the constuctor exits no one can change
the readonly field. Constants on the other hand has no field behind them.
Their value is stored in the assembly metadata and that data is used by the
compiler when generating the code. The values of the constant are hardcoded
once the program is built. The latter might introduce versioning problems
that's why constants should be use for something that doesn't change e.g.
WM_* constants.
The other difference that I feel like needs to be mentioned is that as long
as readonly values can be of any type constants are limited only to the
primitive .NET types.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:lT*******************@news-server.bigpond.net.au...
Michael Culley wrote:
"Jack Hanebach" <ja*****@spam.hanebach.net> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
const means it's not going to change, read-only it might chane but
_you_ cannot change it.

You being the person using the class, if you are the person creating the
class then you can change it. :-)

An object can change the value of its own readonly properties but anything
using that object cannot.
JB

Nov 16 '05 #7
Stoitcho Goutsev (100) [C# MVP] wrote:
Hi,
Actually readonly values can be set only in the constructor of the type
decalring the readonly field. Once the constuctor exits no one can change
the readonly field. Constants on the other hand has no field behind them.
Their value is stored in the assembly metadata and that data is used by the
compiler when generating the code. The values of the constant are hardcoded
once the program is built. The latter might introduce versioning problems
that's why constants should be use for something that doesn't change e.g.
WM_* constants.
The other difference that I feel like needs to be mentioned is that as long
as readonly values can be of any type constants are limited only to the
primitive .NET types.

Yeah, sorry bout that.
I interpreted readonly property as property without a setter since I am
currently (hating it) using vb.net which uses the readonly keyword.

Disregard my post.

JB
Nov 16 '05 #8
The Last Gunslinger <jb******@yahoo.com> wrote:
Stoitcho Goutsev (100) [C# MVP] wrote:
Hi,
Actually readonly values can be set only in the constructor of the type
decalring the readonly field. Once the constuctor exits no one can change
the readonly field. Constants on the other hand has no field behind them.
Their value is stored in the assembly metadata and that data is used by the
compiler when generating the code. The values of the constant are hardcoded
once the program is built. The latter might introduce versioning problems
that's why constants should be use for something that doesn't change e.g.
WM_* constants.
The other difference that I feel like needs to be mentioned is that as long
as readonly values can be of any type constants are limited only to the
primitive .NET types.

Yeah, sorry bout that.
I interpreted readonly property as property without a setter since I am
currently (hating it) using vb.net which uses the readonly keyword.


No, your post was exactly right. Stoitcho's post was describing
readonly *fields*, which may be what the OP meant, but it isn't what he
wrote :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Prasad | last post: by
5 posts views Thread by Paul E Collins | last post: by
2 posts views Thread by Marty | last post: by
8 posts views Thread by Eugene | last post: by
3 posts views Thread by Miro | last post: by
3 posts views Thread by Rob Blackmore | last post: by
7 posts views Thread by Hendrik Schober | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.