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

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

    The returned value is thrown away if not used, but it is created. It is perfectly reasonable not to use it ( although you should be ceratin that this is the right thing to be doing ), but if it takes a lot of resource to create, then this is wasted.

    You may want to consider whether another method would be a better options, that doesn't create the return object at all.

提交回复
热议问题