Const methods in C#

后端 未结 4 633
悲&欢浪女
悲&欢浪女 2021-01-01 08:45

In C++, you can define a constant method like so:

int func_that_does_not_modify_this(int arg) const {}

Placing const at the en

4条回答
  •  鱼传尺愫
    2021-01-01 09:10

    C# 8.0 adds support for C++ style const methods, but only to structs. You can add a readonly modifier to a method deceleration to make any modifications to state within it a compiler warning (which you can define as an error if you wish). A readonly struct method may still call a non-readonly method, but that method will be called on a copy of the struct to prevent any changes to the original data.

    For more information:

提交回复
热议问题