开发者

is there something like C++ const in c#? [duplicate]

开发者 https://www.devze.com 2023-01-31 03:35 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Why const parameters are not allowed in C#
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why const parameters are not allowed in C#

I am a C++ developer and I had some experience using C#.

I have always wondered why there is nothing like this in C#:

bool MyFunction(const AnotherClass& x)
{
  //some stuff
}

I personally think that the constness is one of the best features in C++ (it helps the developer to increase encapsulation and decoupling). One of the suggestion of Sutter (C++ Coding Standards: 101 Rules, Guidelines, and Best Practices) is never return your handlers, if I cannot pass by const reference I am giving away the entire class not just the handlers.

Is it my lack of knowledge on C#? There is anything like 开发者_如何学Gothat in C#? If not, why is C# not using it?


That functionality is not available in C#. You can pass a copy, you can wrap the object in a read only adapter, or you can pass references to immutable objects/structures.

0

精彩评论

暂无评论...
验证码 换一张
取 消