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

后端 未结 10 2112
南方客
南方客 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:44

    From a memory management point of view thats fine - if the calling function doesn't use it, it goes out of scope and gets garbage collected.

    In this particular case DataTable does implement IDisposable so its not all 100% fine:

    If the returned object implements IDisposable then its a good idea to dispose it, for example:

    using (var retVal = InsertIntoDB(...))
    {
        // Could leave this empty if you wanted
    }
    

提交回复
热议问题