11

I'm searching for a way to make it illegal for the user of a method to ignore the returned object/value of a method. In C++, this is possible with the [[nodiscard]] attribute. However, I couldn't find a similar attribute in C#. Is there a standard implementation of such an attribute or a convention on how to achieve this behaviour?

As an example why this may be useful:

var date = new DateOnly(2023, 8, 23);
date.AddDays(10);
if (date == new DateOnly(2023, 8, 23))
{
   Console.WriteLine("AddDays returns a new instance and does not modify the instance itself. [[nodiscard]] would prevent this");
}
stefan
  • 10,215
  • 4
  • 49
  • 90
  • This may help : http://stackoverflow.com/a/1524460/6248956 – YuvShap Oct 11 '16 at 14:33
  • For what i read,[[nodiscard]] is just a proposed attribute (but i'm not up to date in C++). Anyway, I can't see why this behaviour would be useful. Can you give us a example? – Pikoh Oct 11 '16 at 14:36
  • 1
    @Pikoh added example. FYI, nodiscard is in C++17, afaik – stefan Oct 11 '16 at 14:42
  • Just a note: in the dummy sample code, imagine the condition to more complex than to have that covered with a switch statement. – stefan Oct 11 '16 at 14:48
  • 3
    I still don't see in what sense would that nodiscard attribute help in your example. Maybe another one could help – Pikoh Oct 11 '16 at 14:49
  • @Pikoh not thinking about it for a couple of hours made me come to the conclusion that I actually don't want nodiscard, but simply a "this-scope-will-be-returned-from-guarantee" like `if (condition ) { try { whatever(); } finally { return; } }` without the clutter, and of course that I can't return from a finally clause – stefan Oct 11 '16 at 19:03
  • Two general ways of making sure only one thing happens: use a `switch` statement, or use an `Action` field that you only invoke once at the end. Whether either approach is appropriate depends on the code in question. A new keyword would not help in any case: that is no easier to remember or forget than a `return` at the end. – Jeroen Mostert Oct 12 '16 at 09:56
  • Looks similar to https://stackoverflow.com/questions/26853025/force-function-to-return-value-and-make-compile-error-c-sharp – Roman Jul 12 '19 at 13:24
  • @Pikoh For example, I have a function `Task HttpDownloadToFileAsync(url, path);`. Some noob users just calls my function and forgets to wait for finishing. Then the noob would be confused by some magic unstable bug. A compiler warning is required to prevent such errors. – recolic Mar 18 '21 at 08:21

1 Answers1

2

[[nodiscard]] has two analogs in C#/.Net. The first is the poorly named [Pure] attribute which was originally intended to document that a method has no side effects. If the return value of a method marked [Pure] is discarded, code-analysis warning CA1806 is raised in the editor (but not on build by default).

There's also ongoing work to add [return: DoNotIgnore] to the language, but that is as of March 2023 is still in development.

Mitch
  • 21,223
  • 6
  • 63
  • 86