what is use of out parameter in c#

后端 未结 9 1174
南方客
南方客 2021-02-07 16:27

Can you please tell me what is the exact use of out parameter?

Related Question:
What is the differen

9条回答
  •  梦毁少年i
    2021-02-07 16:39

    The best example of a good use of an out parameter are in the TryParse methods.

    int result =-1;
    if (!Int32.TryParse(SomeString, out result){
        // log bad input
    }
    
    return result;
    

    Using TryParse instead of ParseInt removes the need to handle exceptions and makes the code much more elegant.

    The out parameter essentially allows for more than one return values from a method.

提交回复
热议问题