if I run this C# code
int realInt = 3;
string foo = "bar";
Int32.TryParse(foo, out realInt);
Console.WriteLine(realInt);
Console.Read();
I get 0. And I would like to know why! Cause I cannot find any reason why it would. This forces me to make temp variables for every parsing. So please! Great coders of the universe, enlighten me!
It is “out”, not “ref”. Inside the method it has to assign it (without reading it first) to satisfy the meaning of “out”.
Actually, “out” is a language concern (not a framework one) – so a managed C++ implementation could probably ignore this… but it is more consistent to follow it.
In reality; if the method returns false you simply shouldn’t look at the value; treat it as garbage until it is next assigned. It is stated to return 0, but that is rarely useful.
Also – if it didn’t do this (i.e. if it preserved the value); what would this print:
That is perfectly valid C#… so what does it print?