Force function to return value and make compile error C#

后端 未结 4 1090
爱一瞬间的悲伤
爱一瞬间的悲伤 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:54

    You could convert this to a property instead of a method:

      public string myString
      {
        get
        {
          return "Blah";
        }
      }
    

    Then you can't compile if you simply call the property:

    myString.myString; //Results in "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" Error
    

提交回复
热议问题