Why are \'out\' parameters in .NET a bad idea?
I was recently asked this, but I had no real answer besides it\'s simply unnecessarily complicating an application. Wh
I'd say my reason to avoid it, would be because it is simplest to have a method process data and return one logical piece of data. If it needs to return more data it may be a candidate for further abstraction or formalization of the return value.
One exception is if the data returned is somewhat tertiary, like a nullable bool. It can be true/false or undefined. An out parameter can help solve a similar idea for other logical types.
Another one I use sometimes is when you need to get information through an interface that doesn't allow for the 'better' method. Like using .Net data access libraries and ORM for example. When you need to return the updated object graph (after an UPDATE) or new RDBMS-generated identifier (INSERT) plus how many rows were affected by the statement. SQL returns all of that in one statement, so multiple calls to a method won't work as your data layer, library, or ORM only calls one generic INSERT/UPDATE command and can't store values for later retrieval.
Ideal practices like never using dynamic parameter lists, or out parameters, aren't always practical all the time. Again, just think twice if an out parameter is the really right way to do it. If so, go for it. (But make sure to document it well!)