const return type's

S

Steven Dorning

Is there an equivalent way in C# of writing the following
C++ code.

A and B are classes,

class B
{
private:
A m_A;

public:
const A & ReadA () const { return m_A;}
}


i.e. is there a way of returning a reference to a const
object as opposed to a reference to an non-const object.

Many Thanks

Steven
 
J

Jon Skeet [C# MVP]

Steven Dorning said:
Is there an equivalent way in C# of writing the following
C++ code.

A and B are classes,

class B
{
private:
A m_A;

public:
const A & ReadA () const { return m_A;}
}


i.e. is there a way of returning a reference to a const
object as opposed to a reference to an non-const object.

Not in normal .NET, although there is a project to provide a modified
version of Rotor which does it. Whether or not this is a good thing is
a source of much debate. Personally I like the idea of constness, but
can see that *expressing* that is quite tricky (when it come to, say, a
Hashtable where each key is const but the value may not be).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top