When to use ref and out in c#
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 16, 2014, 4:15 AM EDT
This is what ref and out parameters do.
The difference between them is that out parameters do not need to be initialized before being passed to the method but you are guaranteed that they will be assigned a value before the method returns. They are therefore useful when you want to return more than one value from a method.
In contrast ref parameters do need to be initialized before being passed to the method and may or may not be assigned a different value within the method itself.
KalaiRank #8,005 · 230 reputationPosted May 16, 2014, 5:26 AM EDT