Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is c# analog of C++ const reference return value

Tags:

c++

c#

constants

I do write c++ accessor to class member as

SomeClass const& x() const { return m_x; }

It seems that the only protection of this sort in c# is to define property with private (or undefined) set. But this protects only against assignments not against manipulation of the some-class state.

Side note: c++ allows m_x to be deleted through const pointer - IMHO this is simply amazing oversight of standard bodies.

like image 605
uuu777 Avatar asked Oct 30 '22 18:10

uuu777


1 Answers

Now, with C# 7.2, you can use ref readonly for the same purpose. You can check more about that here. Check the third point.

like image 144
germelcar Avatar answered Nov 15 '22 04:11

germelcar