Force function to return value and make compile error C#

后端 未结 4 1025
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 06:23

I have a class and a method within that class. However this class method returns a string. When I call the class method I don\'t get an error even though I\'m not catching

4条回答
  •  囚心锁ツ
    2021-01-18 06:57

    You can try turning on warnings as errors by right-clicking the project in solution explorer, clicking Properties, going to the Build tab and setting Treat warnings as errors to all. This will force you to resolve all warnings before you can build and will capture some of the you-didn't-assign-this scenarios.

    The compiler can't know that the only purpose of your method is to return the string or if it does some work that affects state, and so it can't complain when you don't assign the result to anything.

    You can, however, set it up as a get only property per MikeH's answer. This will complain when you don't assign it to anything.

提交回复
热议问题