Ref Key word
-
As we know we can pass the parameters by ref and also by val. Sometimes we will pass object as reference type even though they are already reference type. Please feel free to say dont be stupid
-
As we know we can pass the parameters by ref and also by val. Sometimes we will pass object as reference type even though they are already reference type. Please feel free to say dont be stupid
You need to pass an object by ref, if the method you're passing it to will replace it with another object. This example is useless, but it shows the effect:
private void DummyReplaceTwoStrings(String s1, String s2){ s1 = "first text"; s2 = "second text"; } private void ReplaceTwoStrings(ref String s1, ref String s2){ s1 = "first text"; s2 = "second text"; } private void CallerMethod(){ String s1 = "hello"; String s2 = "world"; DummyReplaceTwoStrings(s1, s2); Console.WriteLine(s1); Console.WriteLine(s2); ReplaceTwoStrings(ref s1, ref s2); Console.WriteLine(s1); Console.WriteLine(s2); }
_________________________________ Vote '1' if you're too lazy for a discussion -
You need to pass an object by ref, if the method you're passing it to will replace it with another object. This example is useless, but it shows the effect:
private void DummyReplaceTwoStrings(String s1, String s2){ s1 = "first text"; s2 = "second text"; } private void ReplaceTwoStrings(ref String s1, ref String s2){ s1 = "first text"; s2 = "second text"; } private void CallerMethod(){ String s1 = "hello"; String s2 = "world"; DummyReplaceTwoStrings(s1, s2); Console.WriteLine(s1); Console.WriteLine(s2); ReplaceTwoStrings(ref s1, ref s2); Console.WriteLine(s1); Console.WriteLine(s2); }
_________________________________ Vote '1' if you're too lazy for a discussionCorinna John wrote: ReplaceTwoStrings(s1, s2);
ReplaceTwoStrings(ref s1, ref s2);
:-> top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
Corinna John wrote: ReplaceTwoStrings(s1, s2);
ReplaceTwoStrings(ref s1, ref s2);
:-> top secret
Download xacc-ide 0.0.3 now!
See some screenshotsBetter now? You've won a personal extra-ref:
r e f
_________________________________ Vote '1' if you're too lazy for a discussion