0

I stumbled across using 'out' to return more than one variable, but I'm not sure how it works exactly. Would you be able to return 4-5 variables from one method using 'out's? If so, (this is what confused me the most), how does that work? I've looked at a lot of examples, but they are all confusing to me.

If you could include code to demonstrate using an out I would much appreciate it!

Thanks!

Noob
  • 125
  • 1
  • 12
  • 5
    http://msdn.microsoft.com/en-us/library/ee332485(v=vs.110).aspx – ken2k Jul 22 '14 at 13:29
  • http://www.dotnetperls.com/out – ElectricRouge Jul 22 '14 at 13:29
  • ohhh so you just call the method and if you have an out, it will return it? – Noob Jul 22 '14 at 13:30
  • 1
    I almost never use `out`; in most cases it's cleaner just to use an appropriate return type. Note that `out` does not "return" a value, but allows/requires the called function to *modify* a binding (ie variable) in the calling context (this is why `out` arguments must be variables, not arbitrary expressions). – user2864740 Jul 22 '14 at 13:30
  • @user2864740 but if I was trying to return 4 or 5 variables from method how would I do that? – Noob Jul 22 '14 at 13:30
  • @Noob Return a single object (possibly a collection) that represents all the values to return. – user2864740 Jul 22 '14 at 13:31
  • best way to return multiple values is with an user defined object – dwana Jul 22 '14 at 13:32
  • @user2864740 but what if the values are different types? i.e. if I want to return an int and a string? – Noob Jul 22 '14 at 13:33
  • @Noob No problem - `class Person { public string Name { get; set; } public int Age { get; set; } }` (or as appropriate). – user2864740 Jul 22 '14 at 13:34
  • http://msdn.microsoft.com/en-us/library/ms173110.aspx – dwana Jul 22 '14 at 13:35
  • @user2864740 ok thanks! I'll give it a go! if I can't figure it out I'll be back! (As Arnold would say) – Noob Jul 22 '14 at 13:45

0 Answers0