Real-world examples where C# 'out' parameters are useful?

后端 未结 8 882
逝去的感伤
逝去的感伤 2020-12-29 06:46

I\'m reading up on core C# programming constructs and having a hard time wrapping my head around the out parameter modifier. I know what it does by reading but

8条回答
  •  醉话见心
    2020-12-29 07:21

    Simple, when you have a method that returns more than one value. One of the most "famous" cases is Dictionary.TryGetValue:

    string value = "";
    
    if (openWith.TryGetValue("tif", out value))
    {
        Console.WriteLine("For key = \"tif\", value = {0}.", value);
    }
    else
    {
        Console.WriteLine("Key = \"tif\" is not found.");
    }
    

提交回复
热议问题