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#?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

5 Answers5

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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#

Community
  • 1
  • 1
Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
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:

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
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++.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34882423) – Chris Catignani Aug 25 '23 at 16:55