C# out parameters vs returns

后端 未结 5 1724
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:38

    The out keyword is mostly used when you want to return more than one value from a method, without having to wrap the values in an object.

    An example is the Int32.TryParse method, which has both a return value (a boolean representing the success of the attempt), and an out parameter that gets the value if the parsing was successful.

    The method could return an object that contained both the boolean and the integer, but that would need for the object to be created on the heap, reducing the performance of the method.

提交回复
热议问题