C# out parameters vs returns

后端 未结 5 1617
Happy的楠姐
Happy的楠姐 2021-01-11 17:17

So I am new to C# and I am having difficulty understanding out. As opposed to just returning something from a function

using System;
class Retur         


        
5条回答
  •  星月不相逢
    2021-01-11 17:54

    A good use of out instead of return for the result is the Try pattern that you can see in certain APIs, for example Int32.TryParse(...). In this pattern, the return value is used to signal success or failure of the operation (as opposed to an exception), and the out parameter is used to return the actual result.

    One of the advantages with respect to Int32.Parse is speed, since exceptions are avoided. Some benchmarks have been presented in this other question: Parsing Performance (If, TryParse, Try-Catch)

提交回复
热议问题