C# Ref vs. Out
Let me start by saying that if you have to ask yourself this question, the difference may not matter. Where it really does matter in my opinion, is if any of the following are true:
- You are marshalling an object, especially across the internet in a web service
- You are in a multithreaded performance crucial enviroment
Basically, when using ‘Out’, the method creates the object and you don’t have to pass information except where to store the object on the way back and what the object is.
When using ‘ref’ you have to instantiate the object before you can use it. If the other side doesn’t need any information about this object, you probably should just use ‘Out’.
If you are creating interfaces or classes for a framework that is actually going to be used a lot, you might want to think about how the object is going to be used, and decide appropriately.
Filed under: .NET, C# | Leave a Comment
No Responses Yet to “C# Ref vs. Out”