Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK not to handle returned value of a C# method? What is good practice in this example?

Out of curiosity...what happens when we call a method that returns some value but we don't handle/use it? And we also expect that sometimes this returned value could be really big. Where that value goes? Is it even created? If it is, are there any performance issues or other problems that can occur? (what is the best practice in this kind of situation?)

Let's say we have method that does some database operations (insert, update) and returns some data in DataTable object. And I also know that this DataTable object could be really big sometimes:

public static Datatable InsertIntoDB(...)  {       // executing db command, getting values, creating & returning Datatable object...       ...       return myDataTable; } 

And then when this method is used it is called like these:

DataTable myDataTable = InsertIntoDB(...); // this Datatable object is handled in some way 

But sometimes simply like this:

InsertIntoDB(...); // returned value not handled; Problem??? 

On my first thought it think the system is smart enough to see the returned value is ignored and does not cause any problems (it is simply released) but I want to be sure and hear more detailed explanation of it from someone who is more experienced in this area than me.

like image 795
Janez Avatar asked Jul 29 '11 14:07

Janez


People also ask

What happens if you don't return a value?

If you do not explicitly return a value, the caller will nonetheless use whatever garbage happens to be in that register. The compiler will also use all registers it has available for internal computation within the function.

Do you always need a return in C?

Yes. Always do a clean return from any non-void function.

How do you ignore return value?

Many functions return values, but sometimes you don't care what the return value is – you might want to ignore it sometimes, and use it other times. You can use @discardableResult in your own functions. For example, you might write a logging function that accepts a string and optionally also a log level.

What happens when a value is returned?

Nothing. The return value is simply discarded. It doesn't matter what you return, as long as you return something.


1 Answers

The returned value (or reference, if it's a reference type) is pushed onto the stack and then popped off again.

No biggy.

If the return value isn't relevant, you can safely do this.

But be sure that it isn't relevant, just in case.

Here's some code:

    static string GetSomething()     {         return "Hello";     }      static void Method1()     {         string result = GetSomething();     }      static void Method2()     {         GetSomething();     } 

If we look at the IL:

Method1:

.locals init ([0] string result) IL_0000:  nop IL_0001:  call       string ConsoleApplication3.Program::GetSomething() IL_0006:  stloc.0 IL_0007:  ret 

Method2:

IL_0000:  nop IL_0001:  call       string ConsoleApplication3.Program::GetSomething() IL_0006:  pop IL_0007:  ret 

Exactly the same number of instructions. In Method1, the value is stored in the local string result (stloc.0), which is deleted when it goes out of scope. In Method2, the pop operation simply removes it from the stack.

In your case of returning something 'really big', that data has already been created and the method returns a reference to it; not the data itself. In Method1(), the reference is assigned to the local variable and the garbage collector will tidy it up after the variable has gone out of scope (the end of the method in this case). In Method2(), the garbage collector can get to work, any time after the reference has been popped from the stack.

By ignoring the return value, if it really isn't needed, the garbage collector can potentially get to work sooner and release any memory that's been assigned. But there's very little in it (certainly in this case), but with a long running method, hanging onto that data could be an issue.

But far-and-away the most important thing is to be sure that the return value that you're ignoring isn't something that you should be acting on.

like image 81
Steve Morgan Avatar answered Nov 04 '22 14:11

Steve Morgan