gr_arm

Hi,

is it possible to make constant referens to object in C#

in C++ it's look like

const Type& GetNestedData()

{

return m_data;

}

I know that in C# there is not possible directly, is there some other way to send parameter as readonly or constant.

this is my code:

public Matrix this[int i]

{

get

{

return m_Data[i];

}

}

I would like to return data as readonly, I can do it by readonly property , but I need indexing .

so I need something like

public readonly Matrix this[int i]

{

get

{

return m_Data[i];

}

}

but such code not compiling.

Thanks




Re: Visual C# Language constant reference in C#

sirjis

If I understand correctly, you're trying to return an object by reference, but not allow any changes to the object. As far as I know, this is not possible in C#. In C++, I believe a const reference (or is it a reference to a const object ) restricts the code to only allow the methods labeled "const" (indicating they do not change the state of the object) to be called. However, C# syntactically has no way of saying that a method is const, and it doesn't try to figure it out on its own either.

That being said, if you really need to enforce that the object in m_Data[ i ] is not modified, you can have it implement ICloneable and then return m_Data[ i ].Clone(), although there will be a minor performance loss.





Re: Visual C# Language constant reference in C#

OmegaMan

One can do a protected set, which only allows for the value to be changed by the class and no one outside it such as

public string Data
{
get { return _data; }
protected set { _data = value; }
}

which acts as a readonly to the outside world. See C# 2.0: get and set accessor can have different accessibility for more info.




Re: Visual C# Language constant reference in C#

sirjis

I think that using a protected set (or no set) will only prevent code like

Code Snippet
mycollection[2] = new object();

However, it will not prevent:

Code Snippet

mycollection.Add(new ArrayList());

ArrayList myInternalList = (ArrayList)mycollection[mycollection.Count - 1];

myInternalList.Add("hello");

This just changed the object returned by the indexer, which I think is what he's trying to prevent. If not, then just not allowing external set is perfectly fine.





Re: Visual C# Language constant reference in C#

Thomas Danecker

You have to return a read-only wrapper if the object isn't immutable. In the case of lists you can use the System.Collections.ObjectModel.ReadOnlyCollection<T>. Making a mutable object immutable or vice versa isn't possible by design because it would violate the object's contract.




Re: Visual C# Language constant reference in C#

gr_arm

very good Thomas Danecker

so in C# I cant have constant object (reference to constant instant of a some class), am I right

I should wrap it in other class and implement only get accessor

Is there other way to solve this problem

I read that there were a lot of disscutions in MSDN forum about constant object, do u have some links

Thank you !






Re: Visual C# Language constant reference in C#

Peter Ritchie

Thomas is right, you can't use an indexer to do what you want. A "constant" is different than a read-only object. The value returned by an indexer cannot be read-only (or constant) if the type the indexer is returning is a class. If you made Matrix a struct you wouldn't be able to modify it through the indexer (because you'd be returning a value and not a reference) and would get a compile error with something like "myObject[0].MatrixProperty = 1".

If you don't want to make "Matrix" a struct (or can't) you'll have to scrap the idea of an indexer and have a collection property that returns a read-only collection, as detailed by Thomas.