473,387 Members | 1,592 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.

const functions in C# ?

Hi,

can you define const-functions in C# as in C++ ?

for example (C++-code) :

int Cube :: GetSide() const
{
return m_side;
}

Thnx

Christian
Nov 15 '05 #1
20 17289
Chris <ch********@pandora.be> wrote:
can you define const-functions in C# as in C++ ?

for example (C++-code) :

int Cube :: GetSide() const
{
return m_side;
}


No. I don't know whether or not it's being considered. Const-ness can
be very useful, but it does require a fair amount of discipline.

You may well be interested in http://constcli.sscli.net/

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
What is the benifit of a const funtion ?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Chris <ch********@pandora.be> wrote:
can you define const-functions in C# as in C++ ?

for example (C++-code) :

int Cube :: GetSide() const
{
return m_side;
}


No. I don't know whether or not it's being considered. Const-ness can
be very useful, but it does require a fair amount of discipline.

You may well be interested in http://constcli.sscli.net/

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

Nov 15 '05 #3
<di********@discussion.microsoft.com> wrote:
What is the benifit of a const funtion ?


On its own, not a lot - but when combined with the idea of a const
object or variable, it means you can have more safety. For instance,
suppose you maintain a byte array within a class, and you wish to make
that array available in a "read-only" kind of way - you could expose it
with a "const" property, which would mean that clients would have to
do:

const byte[] foo = myObject.MyProperty;

instead of

byte[] foo = myObject.MyProperty;

They then couldn't do

foo[0] = 5;

but they could do

byte x = foo[0];

It means you can basically return references to mutable objects, but
with the "const" modifier meaning that the client could only call
methods marked as "const" as well. (So for instance, in StringBuilder
ToString might be marked as "const", but Append wouldn't.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
isnt that why we have the readonly keyword? Setonce then its locked from
setting.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<di********@discussion.microsoft.com> wrote:
What is the benifit of a const funtion ?


On its own, not a lot - but when combined with the idea of a const
object or variable, it means you can have more safety. For instance,
suppose you maintain a byte array within a class, and you wish to make
that array available in a "read-only" kind of way - you could expose it
with a "const" property, which would mean that clients would have to
do:

const byte[] foo = myObject.MyProperty;

instead of

byte[] foo = myObject.MyProperty;

They then couldn't do

foo[0] = 5;

but they could do

byte x = foo[0];

It means you can basically return references to mutable objects, but
with the "const" modifier meaning that the client could only call
methods marked as "const" as well. (So for instance, in StringBuilder
ToString might be marked as "const", but Append wouldn't.)

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

Nov 15 '05 #5
<di********@discussion.microsoft.com> wrote:
isnt that why we have the readonly keyword? Setonce then its locked from
setting.


No, readonly will make the *variable* readonly. It says nothing about
the object that the reference in that variable refers to (which is why
you can't mark a method as returning a "readonly" reference).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
100
Hi Jon,
This is not the idea behind the const members of a class in c++. What you
are talking about is method returning const object.
When a method is declared as *const* (in a way Chris did it in his original
post) that means that this method won't modify anything, but its local
variables nor call any function which is not declared as *const* but it has
nothing to do with the constness of the returned object. It just guarantee
that the method itself will not change anything.

So to answer Chris's question: No, C# doesn't have *const* methods.

B\rgds
100

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<di********@discussion.microsoft.com> wrote:
What is the benifit of a const funtion ?


On its own, not a lot - but when combined with the idea of a const
object or variable, it means you can have more safety. For instance,
suppose you maintain a byte array within a class, and you wish to make
that array available in a "read-only" kind of way - you could expose it
with a "const" property, which would mean that clients would have to
do:

const byte[] foo = myObject.MyProperty;

instead of

byte[] foo = myObject.MyProperty;

They then couldn't do

foo[0] = 5;

but they could do

byte x = foo[0];

It means you can basically return references to mutable objects, but
with the "const" modifier meaning that the client could only call
methods marked as "const" as well. (So for instance, in StringBuilder
ToString might be marked as "const", but Append wouldn't.)

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

Nov 15 '05 #7
100 <10*@100.com> wrote:
This is not the idea behind the const members of a class in c++. What you
are talking about is method returning const object.
When a method is declared as *const* (in a way Chris did it in his original
post) that means that this method won't modify anything, but its local
variables nor call any function which is not declared as *const* but it has
nothing to do with the constness of the returned object. It just guarantee
that the method itself will not change anything.


But there are two things here - the method could be declared not to
change anything in the current object, *and* it could also be declared
to return a reference to an object which then can't be changed by the
client. In other words, if something returns const Foo, then only the
methods declared as const within Foo are available.

I don't think you get anything like the full benefit without both
halves.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
>"When a method is declared as *const* (in a way Chris did it in his
original
post) that means that this method won't modify anything but its local
variables, nor call any function which is not declared as *const* "
That was my idea indeed about the const-function.

So, unfortunately it doesn't exits in C# ? too bad.

thanks to you all for your time though !!

Christian

"100" <10*@100.com> wrote in message
news:Os**************@TK2MSFTNGP11.phx.gbl... Hi Jon,
This is not the idea behind the const members of a class in c++. What you
are talking about is method returning const object.
When a method is declared as *const* (in a way Chris did it in his original post) that means that this method won't modify anything, but its local
variables nor call any function which is not declared as *const* but it has nothing to do with the constness of the returned object. It just guarantee
that the method itself will not change anything.

So to answer Chris's question: No, C# doesn't have *const* methods.

B\rgds
100

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<di********@discussion.microsoft.com> wrote:
What is the benifit of a const funtion ?


On its own, not a lot - but when combined with the idea of a const
object or variable, it means you can have more safety. For instance,
suppose you maintain a byte array within a class, and you wish to make
that array available in a "read-only" kind of way - you could expose it
with a "const" property, which would mean that clients would have to
do:

const byte[] foo = myObject.MyProperty;

instead of

byte[] foo = myObject.MyProperty;

They then couldn't do

foo[0] = 5;

but they could do

byte x = foo[0];

It means you can basically return references to mutable objects, but
with the "const" modifier meaning that the client could only call
methods marked as "const" as well. (So for instance, in StringBuilder
ToString might be marked as "const", but Append wouldn't.)

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


Nov 15 '05 #9
100
> I don't think you get anything like the full benefit without both
halves.


Disagree. The fact that I'm calling *const* doesn't necessarily mean that I
don't want to change the returned object as well as the fact that the class
provides a method that is not *const* doesn't necessarily mean that the
returned value has to be changeable.
For example Clone method can be decalred as *const*. As user of this method
it gives me peace of
mind that this method won't change anything. As programmer it helps me to
avoid errors. Anyway it returns copy that the user can change.
Or if operators -- and ++ were prototyped as *const* methods. Errors like
"not creating new object but altering the one passed via method parameter"
will not occur. It doesn't mean though that the returned object cannot be
changed.

B\rgds
100
Nov 15 '05 #10
100 wrote:
Disagree. The fact that I'm calling *const* doesn't necessarily mean
that I don't want to change the returned object as well as the fact
that the class provides a method that is not *const* doesn't
necessarily mean that the returned value has to be changeable.


But isn't the whole point of const methods and methods returning const
references to allow that decision to be made by the designer, not the
user?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #11
100
Hi Frank,
Disagree. The fact that I'm calling *const* doesn't necessarily mean
that I don't want to change the returned object as well as the fact
that the class provides a method that is not *const* doesn't
necessarily mean that the returned value has to be changeable.


But isn't the whole point of const methods and methods returning const
references to allow that decision to be made by the designer, not the
user?

Returning *const* object the designer force not changing the object. But my
point is that const method has nothing to do with returning const object.
The retuned object from *const* method might be *const*, but doesn't have to
be.

From the users point of view method declared as *const* has more informative
meaning.

For method returning *const* - Yes, designer makes the desicion. And that is
what Jon was talking about.
But the question was about the *const* methods. And with *const* methods
designer doesn't make any desicions about the returned object.

B\rgds
100
Nov 15 '05 #12
100 <10*@100.com> wrote:
I don't think you get anything like the full benefit without both
halves.
Disagree. The fact that I'm calling *const* doesn't necessarily mean that I
don't want to change the returned object as well as the fact that the class
provides a method that is not *const* doesn't necessarily mean that the
returned value has to be changeable.


You don't have to have both halves in the same *method*, and I never
suggested that you did.
For example Clone method can be decalred as *const*. As user of this method
it gives me peace of mind that this method won't change anything.
That can be done with documentation though - the main benefit of
constness (IMO) is getting the compiler to notice that something you've
said won't change genuinely won't, and making sure you don't try to
change something you shouldn't do.
As programmer it helps me to avoid errors.
How, if the compiler isn't going to enforce anything? Without both
halves, the compiler hasn't *got* anything to enforce.
Anyway it returns copy that the user can change.
Or if operators -- and ++ were prototyped as *const* methods. Errors like
"not creating new object but altering the one passed via method parameter"
will not occur. It doesn't mean though that the returned object cannot be
changed.


No, and that's why I've said, they're separate things - but I *do*
believe you don't get much benefit unless both parts are available.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #13
100 <10*@100.com> wrote:
Returning *const* object the designer force not changing the object. But my
point is that const method has nothing to do with returning const object.
Yes it does - because when you've had a const object reference returned
to you, you should only be able to call const methods.
The retuned object from *const* method might be *const*, but doesn't have to
be.
And no-one's claimed that it does.
From the users point of view method declared as *const* has more informative
meaning.

For method returning *const* - Yes, designer makes the desicion. And that is
what Jon was talking about.
But the question was about the *const* methods. And with *const* methods
designer doesn't make any desicions about the returned object.


Yes - but any explanation of const methods which doesn't also include
how const variables/objects/references (I'm not so hot on the exact
terminology here) come about in the first place, which will often
include a method returning a const object reference, is incomplete IMO.
You seem to think I'm conflating the two ideas, and I'm not - but to
say they have "nothing to do" with each other is wrong, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
100
> > From the users point of view method declared as *const* has more
informative
meaning.

For method returning *const* - Yes, designer makes the desicion. And that is what Jon was talking about.
But the question was about the *const* methods. And with *const* methods
designer doesn't make any desicions about the returned object.
Yes - but any explanation of const methods which doesn't also include
how const variables/objects/references (I'm not so hot on the exact
terminology here) come about in the first place, which will often
include a method returning a const object reference, is incomplete IMO.
You seem to think I'm conflating the two ideas, and I'm not - but to
say they have "nothing to do" with each other is wrong, IMO.


If the question was about the const variables or reference to const objects
(or in terms of c++ const variables, const members or pointers and
references to const objects) - yes, you should say something about const
methods as long as those are the only methods of the object that can be
called. But talking about *const* methods we may not mention const
variables, etc because those methods may use any type of variables and
members. They are not limited to access const fields or (as we both agree)
of returning const objects only.

B\rgds
100
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #15
100 <10*@100.com> wrote:
If the question was about the const variables or reference to const objects
(or in terms of c++ const variables, const members or pointers and
references to const objects) - yes, you should say something about const
methods as long as those are the only methods of the object that can be
called. But talking about *const* methods we may not mention const
variables, etc because those methods may use any type of variables and
members. They are not limited to access const fields or (as we both agree)
of returning const objects only.


They're not *limited* to accessing const fields or returning const
objects, but I would argue that one of the great benefits of const
methods is that you can "label" methods which are safe to call on
objects which you don't really own - and that the compiler can check
that. The compiler *can't* check that, however, if you don't have any
concept of const variables to start with.

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

<snip>

Looking back at some of my previous posts, I certainly gave less-than-
great examples - a better example would be something like Hashtable,
where the CopyTo method could be const, but Clear wouldn't be. A method
in a user class may wish to return a const Hashtable reference, which
could then only have const methods (such as CopyTo) invoked on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17
100
Hi Jon,
Looking back at some of my previous posts, I certainly gave less-than-
great examples - a better example would be something like Hashtable,
where the CopyTo method could be const, but Clear wouldn't be. A method
in a user class may wish to return a const Hashtable reference, which
could then only have const methods (such as CopyTo) invoked on it.


That is better ;) I agree in 100% with this example.
My point is that we hardly can have const objects beside the primitive ones
(which are used as litterals and .NET already has) without the consept of
const methods. But we can still have const methods without the concept of
const objects. Of course it is better to have them both.
Anyway I'm kind of sceptic about this in .NET since all languages in the
familiy would have supported them. Otherwise reusing code written in a
language, which has this concept from code, which doesn't, would either
compromise the const-ness or wouldn't be able to reuse the code.

B\rgds
100
Nov 15 '05 #18
100 <10*@100.com> wrote:
Looking back at some of my previous posts, I certainly gave less-than-
great examples - a better example would be something like Hashtable,
where the CopyTo method could be const, but Clear wouldn't be. A method
in a user class may wish to return a const Hashtable reference, which
could then only have const methods (such as CopyTo) invoked on it.
That is better ;) I agree in 100% with this example.
My point is that we hardly can have const objects beside the primitive ones
(which are used as litterals and .NET already has) without the consept of
const methods. But we can still have const methods without the concept of
const objects. Of course it is better to have them both.


Right :)
Anyway I'm kind of sceptic about this in .NET since all languages in the
familiy would have supported them. Otherwise reusing code written in a
language, which has this concept from code, which doesn't, would either
compromise the const-ness or wouldn't be able to reuse the code.


True - I hadn't thought of that. This is the kind of thing which makes
a multi-language platform tricky... but the same is true of generics,
presumably.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #19
100
Anyway I'm kind of sceptic about this in .NET since all languages in the
familiy would have supported them. Otherwise reusing code written in a
language, which has this concept from code, which doesn't, would either
compromise the const-ness or wouldn't be able to reuse the code.


True - I hadn't thought of that. This is the kind of thing which makes
a multi-language platform tricky... but the same is true of generics,
presumably.


I haven't had time to go through the c#'s generics specs. So I can't tell
anything about it.
But you are right a multi-language platform could be indeed very tricky.

B\rgds
100
Nov 15 '05 #20
Hi,

http://groups.google.com/groups?hl=e....phx.gbl#link1

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

"Chris" <ch********@pandora.be> wrote in message
news:TV*********************@phobos.telenet-ops.be...
Hi,

can you define const-functions in C# as in C++ ?

for example (C++-code) :

int Cube :: GetSide() const
{
return m_side;
}

Thnx

Christian

Nov 15 '05 #21

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

Similar topics

2
by: Mike P. Wagner | last post by:
I have a real world problem that I hope someone has solved. I implementing a class in a kernel that stores (and reads) data persistently. There is one underlying "raw IO" function that calls the...
1
by: Luis | last post by:
can you look at my code, and tell my wy the section of the client program which says : const Fraction f3(12, 8); const Fraction f4(202, 303); result = f3.MultipliedBy(f4); cout << "The product...
11
by: Romulus Mare | last post by:
How can I avoid writting and maintaining the same code in the two operator implementation below? I know that in the minimal example below, the implementation is trivial, but I found this type of...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
6
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
5
by: arun | last post by:
Hello Team, I have a question on const vs non const and object return vs non object return function and which one will be called ie. void foo(); vs void foo() 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.