68

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

int func_that_does_not_modify_this(int arg) const {}

Placing const at the end of the function prevents you from accidentally modifying any of the internal properties, and lets the caller know that this function won't modify the object.

Is there a concept like this in C#?

5 Answers 5

66

No, there's nothing like that in C#. It's been talked about a lot, but it's quite difficult to make const work in such a way that it's verifiable at compile time, can't be cast away like it can in C++, and is still reasonably easy to actually use without everyone having to get it perfectly right when they design their own classes.

Of course, if you design your own types to be immutable (like string) then all instance methods on it are effectively const. This isn't always practical, but it's an important technique to use where appropriate.

2
  • 5
    Any news on this, Jon ?
    – Artyom
    Mar 28, 2020 at 13:42
  • 7
    @Artyom: Not that I'm aware of, beyond readonly structs to help things a little.
    – Jon Skeet
    Mar 28, 2020 at 13:57
20

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#

0
10

No. There's nothing similar in C#.

No const & either.

As Jon points out you can obviously implement a const method, but there's no way beyond documentation to let the caller know that a method is const.

1
  • 2
    What about in keyword? Isn't it comparable to const ref?
    – stephanmg
    Feb 15, 2020 at 16:48
9

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:

1

It's not quite the same thing, but if a method doesn't need any access to a class's instance data, you can make it static. That will at least convey to the caller that the method is conceptually "part" of the class, but doesn't modify instance data (because there is no instance). I've seen lots of cases where a class had instance methods that were "inherently" static, but just weren't marked that way. Once I added the keyword I was at least able to make a set of assumptions about the method that weren't possible before.

Incidentally, the same advice holds true for C++.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.