Const methods in C#

后端 未结 4 636
悲&欢浪女
悲&欢浪女 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 08:50

    Code Contract should provide such a feature in the future. Currently, you can mark a method as [Pure], which means it doesn't have any side-effects (i.e. doesn't modify any of the class members). Unfortunately, the current version of the tools does not enforce this rule, so using that attribute is for documentation purpose only. I'm pretty sure that in future version, it will be enforced via static-analysis (i.e. at compile-time), or at least that's what the documentation hints at.

    Related SO questions: Pure functions in C#

提交回复
热议问题