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

后端 未结 10 2114
南方客
南方客 2020-12-07 23:49

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 rea

10条回答
  •  心在旅途
    2020-12-08 00:45

    I'm certain that this doesn't cause any problems, otherwise C# wouldn't be a very reliable language.

    I'm guessing the compiler isn't smart enough to optimize this. What most likely happens is the ordinary logic inside the function call is executed, e.g. creating the object and allocating memory for it. If a reference type is returned but not captured, garbage collection will free up the memory again.

    As others have stated, from a design view ignoring the return value does indicate a problem and most likely you should be looking at the return value.

提交回复
热议问题