LogFAQs > #909988684

LurkerFAQs, Active DB, DB1, DB2, DB3, Database 4 ( 07.23.2018-12.31.2018 ), DB5, DB6, DB7, DB8, DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicCool function I wrote
Sahuagin
10/05/18 2:22:33 AM
#20:


Magus 10 posted...
You should never use ref unless you have a very good reason why (which 90% of the time is PInvoke, and the other 10% can probably be done a better way with tuples or something; edit: actually that's more likely the solution for out params, but I can't think of anything else for the other 10%).


one use for ref would be maybe if you had a counter that you wanted to pass between methods. then you can share the counter between a bunch of methods and increment it in any one of them. but then you can just put it in an object, too, which is better, so it's not super useful.

there are definitely some good uses for out parameters though, especially now that they aren't so clumsy in C# 7 with out var.

the standard example is something like:

if (!int.TryParse(someText, out var intValue))
...return;

// do something with intValue

---
... Copied to Clipboard!
Topic List
Page List: 1