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");
}