Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language

Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Is const allowed anywhere?


I have just started learning C# ; I was working in C++ till now (and
would like to if allowed, but learning new things dont hurt). I am
used to specifiying "const" to get the assurance that unless someone
specifically cast-away the constness, object wont change. So, as part
of my first ever program I wrote a following class -

public class FirstOne {
        public int intTry;
        public FirstOne() {
            intTry = 0;

        //public FirstOne(readonly FirstOne o)
        //public FirstOne(const FirstOne o)
        public FirstOne(FirstOne o) {
            intTry = o.intTry;
            o.intTry = -100; // This would be possible if I can't make
the parameter const..


Since class in C# is reference type (still getting hang of the
concept) object 'o' is already a reference, I am just trying to make
it const so that it doesn't change. But the syntax (commented lines)
does not work. Is it even possible to have const ref or it's just a
glitch in my C# syntax understanding?

Thanks in advance,

No is the short answer; if a class is mutable (which yours is), then
any caller can update properties (or fields if they have access).

You could perhaps make the class immutable, but this prevents anybody
from changing the contents (you need to recreate the object from
scratch to "change" it). You could create an interface with only the
"get" accessors, but that doesn't prevent you from casting back to the
real type.

Otherwise you could perhaps pass clones around, but that is an
overhead and relies on the various parties playing ball. There is no
C# syntax to do what you want (treat a mutable object as immutable
within a scope).



Just in case I missed your meaning; you can do the following

public readonly int intTry;

now *only* the constructor can set intTry.

Also - can I recommend using properties instead of direct fields? In
simple cases this will be inlined by the JIT anyway, but gives you a
lot more flexibility to add functionality; as an example (with a

private void CheckEditable() {
   // allow some kind of lock/unlock mechanism...
    if(!IsEditable) throw new InvalidOperationException("The record is
not currently editable");


private int shoeSize;
public int ShoeSize {
  get {return shoeSize;}
  set {
    if(value==ShoeSize) return; // no change
    if(value < 0 || value > 20) throw new
    shoeSize= value;
    OnPropertyValueChanged("Age"); // "observer" notification

Thanks Marc.

> now *only* the constructor can set intTry.

This is not my intention. I just want to have option of sending object
of this class as read-only/const object to methods/constructors when
required. I believe what you said in your earlier post , "No is the
short answer", is the short but correct answer :-)

Even if I have just started learning it now, I can't help but wonder
why it was left out..

> Also - can I recommend using properties instead of direct fields?

Yes, what you said about using properties instead of fields makes much
more sense.


Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc