473,387 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Is const allowed anywhere?

Hi,

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,
Neel.

Jun 7 '07 #1
8 1225
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).

Marc
Jun 7 '07 #2
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
use-case)...

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
ArgumentOutOfRangeException("something");
CheckEditable();
shoeSize= value;
OnPropertyValueChanged("Age"); // "observer" notification
}
}
Jun 7 '07 #3
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.

Thanks,
Neel.

Jun 7 '07 #4
<ne*******@rediffmail.comwrote:

<snip>
Even if I have just started learning it now, I can't help but wonder
why it was left out..
From http://www.artima.com/intv/choicesP.html

<quote>

Anders Hejlsberg: Yes. With respect to const, it's interesting, because
we hear that complaint all the time too: "Why don't you have const?"
Implicit in the question is, "Why don't you have const that is enforced
by the runtime?" That's really what people are asking, although they
don't come out and say it that way.

The reason that const works in C++ is because you can cast it away. If
you couldn't cast it away, then your world would suck. If you declare a
method that takes a const Bla, you could pass it a non-const Bla. But
if it's the other way around you can't. If you declare a method that
takes a non-const Bla, you can't pass it a const Bla. So now you're
stuck. So you gradually need a const version of everything that isn't
const, and you end up with a shadow world. In C++ you get away with it,
because as with anything in C++ it is purely optional whether you want
this check or not. You can just whack the constness away if you don't
like it.

</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 7 '07 #5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
<ne*******@rediffmail.comwrote:

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

From http://www.artima.com/intv/choicesP.html

<quote>

Anders Hejlsberg: Yes. With respect to const, it's interesting, because
we hear that complaint all the time too: "Why don't you have const?"
Implicit in the question is, "Why don't you have const that is enforced
by the runtime?" That's really what people are asking, although they
don't come out and say it that way.

The reason that const works in C++ is because you can cast it away. If
you couldn't cast it away, then your world would suck. If you declare a
method that takes a const Bla, you could pass it a non-const Bla. But
if it's the other way around you can't. If you declare a method that
takes a non-const Bla, you can't pass it a const Bla. So now you're
stuck. So you gradually need a const version of everything that isn't
const, and you end up with a shadow world. In C++ you get away with it,
because as with anything in C++ it is purely optional whether you want
this check or not. You can just whack the constness away if you don't
like it.

</quote>
And he is totally wrong. He appeals to the refactoring a library written
with const -- designing for const-correctness carries none of his presumed
drawbacks. I never use const_cast<in my programs, nor a C-style cast to
do the same... if a third-party library doesn't accept a const pointer when
I think it should, I assume it is reserving the right to overwrite that
buffer, and I need to make a copy for it to act on. That option is always
available, would be even easier in .NET with the Clone() method, and is
safe.
>
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 8 '07 #6
I do firmly believe that I should have a way to be sure that the
object that I am sending in will not change under normal circumstances
and should be able to assure the user of my class that if I am the
class writer. I would have imagined that instead of not allowing
"const", it would not provide casting (in any form) if the object is
const.

I am similarly confused by the fact that (taking example I have
mentioned), only user defined reference type const variable I can
create is -
const FirstOne = null;

since I am not sure what am I supposed to do with it. But then, that
can be gap in my understanding as far as use of it is concerned.

Thanks,
Neel.

Jun 11 '07 #7
Re the "const reference type" variable... the main thing here is that
const expressions are evaluated at compile-time (not run-time), and
hence they can't really do much with objects - just literals. Strings
are treated a little sneakier.

In the case you are talking about, a readonly static field might be in
order, perhaps initialised in a static ctor if the initialisation is
non-trivial (or inline with the field declaration if it is simple).
But note that this *still* doesn't stop you from changing properties
of the object - it only stops you from changing the reference itself.
Hence the object would ideally be immutable in this case.

Some classes exhibit a "make a readonly copy / make me readonly"
behavior for this, but it itsn't common.

i.e.

class SomeClass {

static readonly someStaticField
static SomeClass() { // static ctor
someStaticField = SomeWayOfCreatingTheStaticInstance();
}
public SomeClass() {}// instance ctor

void SomeMethod() {
// make use of someStaticField
}

}

Marc
Jun 11 '07 #8
Thanks Marc.

I read about the approach you are referring to, but, again, all I want
to do is make a new object I am about to create a constant object. I
may be wrong but if I consider making object read-only even that has
to be a class level object which may not be needed (I tried to make
const object in local scope through read-only and C# didnt like
it :) ). I would have thought that, keyword "mutable" will be absent
so that C# doesn't become another version of C++. I think what I am
getting at is, reducing complexity of all the options and their
permutations/combinations, MAY BE, was not required to be taken this
far. For example, in C++, if a function is expecting a const objec/
reference, I only have to rely on parameters if the library/class
source code isn't present, which can become headache later because in
some case library user can simply cast away const-ness. But since C#
does not allow const ref to be passed at all, all I can do is make a
clone and it may (most probably even) turn out to be expensive. But
that's just what I think... :)

Thanks,
Neel.

Jun 11 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
8
by: Nobody | last post by:
The requirement that STL container elements have to be assignable is causing me a problem. Consider a class X which contains both const and non-const data members: class X { public: X(const...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
7
by: rahul8143 | last post by:
hello, const int *ptr1 mean i can change value of ptr1 but not of *ptr1 right? then why following snippet doesnot give error? int val=50; const int *ptr1=&val; *(int *)ptr1=98; //what is this...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
13
by: ragged_hippy | last post by:
Hi, I wanted to create a vector of const references. Something like this: vector<const x&y; where x is a class name. Is this a valid statement? Will this work if I use a reference of 'y'...
7
by: JPS | last post by:
Can someone please refresh my memory here, but what is the difference between a Static variable (or method) and a Const?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.