constant vs. read only property

  • Thread starter Thread starter Davíð Þórisson
  • Start date Start date
D

Davíð Þórisson

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??
 
Davmp said:
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.
 
Jack Hanebach said:
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
 
Michael said:
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
 
'readonly' variables can only be assigned a value in the declaration
or in the variable's class constructor.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
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??<
 
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.
 
Stoitcho said:
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
 
The Last Gunslinger said:
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 :)
 
Back
Top