Monday, 12 February 2018

c# - What's the difference between the 'ref' and 'out' keywords?




I'm creating a function where I need to pass an object so that it can be modified by the function. What is the difference between:



public void myFunction(ref MyClass someClass)


and



public void myFunction(out MyClass someClass)



Which should I use and why?


Answer



ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.



So while ref is two-ways, out is out-only.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...