8

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 the string value return. Is there a way that I can make C# and .net force me to capture the value when returning a value. Here is an example of what I mean.

1- create a class test.

class test
    {

        public string mystring() {

            return "BLAH";

        }
    }

2- call the class method from program or another class

test mystring = new test();
mystring.mystring();

My compiler while working in Visual Studio does not complain that I'm not capturing the return value. Is this normal or I'm missing something. Can I force the compiler to notify me that the function returns a value but I'm not catching it?

Thanks in advance for any suggestions you may have.

Roman
  • 515
  • 5
  • 16
Miguel
  • 2,019
  • 4
  • 29
  • 53

5 Answers5

9

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
MikeH
  • 4,242
  • 1
  • 17
  • 32
  • 1
    I don't understand what you mean dognose? if you tried to do in this case `mystring.mystring;` (note no ()'s as it is now a property not a method) that will not compile. – Nyra Nov 10 '14 at 21:21
  • @dognose: The compiler *cannot* force you to act on the return value. That falls under computing theory. There are 9 things that a computer *cannot* do. (Don't ask me to list them, it's been 20 years since my computer theory class.) But suffice to say that the compiler cannot know that you are making any kind of effective use of the return value. – DeadZone Nov 10 '14 at 21:29
  • but what if i need the method to do something like compute somevalues and then return "BLAH" can i still get away with a property? – Miguel Nov 10 '14 at 21:29
  • Yes. Properties can perform calculations within them just like methods can. – DeadZone Nov 10 '14 at 21:31
  • 1
    @Miguel, in short: yes. However, typically you only want to do "fast" operations in properties. See [here](http://msdn.microsoft.com/en-us/library/vstudio/ms229054(v=vs.100).aspx) for more info – MikeH Nov 10 '14 at 21:31
  • 2
    @Miguel, you can also have a property return a method, so `string mystring{ get { return test(); }}` ..... `test(){ return "test";}` calling `mystring` would give you "test" and you would have to use it. – Nyra Nov 10 '14 at 21:33
1

In a word, no. Not by force as such.

It's commonplace to not capture return values. Examples in the core libs abound (adding elements to a Hashset<T> for example, the function actually returns a bool to flag whether it was actually added or if it already existed - depending on individual implementation I may or may not care about that).

Of course, you can always just do something like string str = MyFunction() each time then never use str but I guess you probably already knew that.

Stewart_R
  • 13,764
  • 11
  • 60
  • 106
1

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.

Steve Lillis
  • 3,263
  • 5
  • 22
  • 41
0

If your function has side effects then your should create unused variable and catch value. Compiler on release options delete this variable.

But if you don't have side effects in function: you may use visual studio tools such as "Watch window" and "Immediate window"

Evgeniy Mironov
  • 777
  • 6
  • 22
0

For some future person who comes across this issue, you can use an 'out'. This will force you to always assign the variable.

public void mystring(out returnString) {

    returnString = "BLAH";

}

...

string OutString;
test mystring = new test();
mystring.mystring(out OutString);
  • 2
    Well, `mystring(out _);` would still compile and discard the result without assigning anything. It is at least explicit though. – Jon Skeet May 08 '22 at 07:09