return - Returning Multiple Value with the 'out' Keyword in C# -


i'm struggling understand means when it's stated 'out' keyword we're able return multiple values. example msdn site (https://msdn.microsoft.com/en-us/library/ee332485.aspx): "...the following examples uses out return 3 variables single method call."

class outreturnexample {     static void method(out int i, out string s1, out string s2)     {         = 44;         s1 = "i've been returned";         s2 = null;     }     static void main()     {         int value;         string str1, str2;         method(out value, out str1, out str2);         // value 44         // str1 "i've been returned"         // str2 (still) null;     } } 

i'm not sure if i'm not reading description right seems method() doesn't return (doesn't use 'return' keyword) @ , assigns fields (similarly through passing ref). consistent other sources state using 'out' can return multiple values. misunderstanding context of return word or along lines i'm not understanding concept properly?

the method indeed not return value noticed correctly. both ref , out work references.

ref let compiler know variable should initialized before enters function (this useful when used variable before function , want change now). out let compiler know object initialized inside function it's calling. ref works both ways, out out-only.

so, yes, not return something. on other hand, assign values variables through calling method ends new values. can see return.

so in short:

  • ref parameters - these have same capabilities c++ reference parameters, ,

  • out parameters - these allow passing data method, not method.

i suggest read this answer , corresponding blog of jon skeet parameter passing. give lot of information concept. , jon skeet notes, careful when using ref , out:

it's way of getting return value, , should avoided precisely because means method's trying much.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -