Click here to Skip to main content
15,912,204 members
Home / Discussions / C#
   

C#

 
GeneralRe: Ensure a parameter is not null Pin
Vikram A Punathambekar13-May-08 19:06
Vikram A Punathambekar13-May-08 19:06 
AnswerRe: Ensure a parameter is not null Pin
Christian Graus13-May-08 11:45
protectorChristian Graus13-May-08 11:45 
GeneralRe: Ensure a parameter is not null Pin
Malcolm Smart13-May-08 12:35
Malcolm Smart13-May-08 12:35 
GeneralRe: Ensure a parameter is not null Pin
Christian Graus13-May-08 13:23
protectorChristian Graus13-May-08 13:23 
GeneralRe: Ensure a parameter is not null Pin
S. Senthil Kumar13-May-08 17:36
S. Senthil Kumar13-May-08 17:36 
GeneralRe: Ensure a parameter is not null Pin
Vikram A Punathambekar13-May-08 19:08
Vikram A Punathambekar13-May-08 19:08 
GeneralRe: Ensure a parameter is not null Pin
Malcolm Smart13-May-08 21:24
Malcolm Smart13-May-08 21:24 
GeneralRe: Ensure a parameter is not null Pin
S. Senthil Kumar13-May-08 22:38
S. Senthil Kumar13-May-08 22:38 
Malcolm Smart wrote:
Like CG said, they are by ref, by default. Both instances will change.


Yes, but as you pointed out, passing the parameter with "ref" allows you to change the reference itself - which is different from changing the object being referenced. In your example, both methods only change the object being referenced, and there is no difference in that case.

My point was passing reference types for "ref" has a different meaning than passing them without "ref".

And strings don't behave differently - strings are immutable reference types. In your example,
static void changeMyString1(string myString)
        {
            myString = mystring + " worked";
        }

        static void changeMyString2(ref string myString)
        {
            myString = mystring + " worked"; 
        }


the second method takes myString by ref, and therefore changes the reference itself. After both methods have executed, mystring will be referring to a new string object with " worked" concatentated. By passing myString as ref in the second method, the "original" myString that was passed to changeMyString2 changes to refer to the new string object. The first method doesn't take myString by ref, and therefore the original myString passed doesn't change.

If you know C++, the following piece of code demonstrates the difference.
int global = 10;

void Test()
{
   int val = 0;
   
   int *p = &val;
   Method2(p); // *p = 0;
   Method1(&p); // *p = 10
}

void Method1(int **p)
{
  *p = &g;
}

void Method2(int *p)
{
   p = &g;
}


You can consider p to be equivalent to a reference type. Method2 is equivalent to not passing with "ref", and Method1 is equivalent to passing with "ref". Changing p inside Method2 doesn't affect p in Test, but changing *p in Method1 does.

Regards
Senthil [MVP - Visual C#]
_____________________________
My Blog | My Articles | My Flickr | WinMacro

GeneralRe: Ensure a parameter is not null Pin
Vikram A Punathambekar13-May-08 19:12
Vikram A Punathambekar13-May-08 19:12 
AnswerRe: Ensure a parameter is not null Pin
PIEBALDconsult13-May-08 13:23
mvePIEBALDconsult13-May-08 13:23 
QuestionApp.Config Sharing (sort of) Pin
#realJSOP13-May-08 5:17
professional#realJSOP13-May-08 5:17 
AnswerRe: App.Config Sharing (sort of) Pin
Miszou13-May-08 7:12
Miszou13-May-08 7:12 
GeneralRe: App.Config Sharing (sort of) Pin
#realJSOP13-May-08 8:31
professional#realJSOP13-May-08 8:31 
GeneralRe: App.Config Sharing (sort of) Pin
Miszou13-May-08 9:04
Miszou13-May-08 9:04 
GeneralRe: App.Config Sharing (sort of) Pin
#realJSOP13-May-08 10:57
professional#realJSOP13-May-08 10:57 
GeneralRe: App.Config Sharing (sort of) Pin
Miszou14-May-08 6:45
Miszou14-May-08 6:45 
QuestionException Handling Pin
jchigg200013-May-08 5:13
jchigg200013-May-08 5:13 
AnswerRe: Exception Handling Pin
jchigg200013-May-08 5:43
jchigg200013-May-08 5:43 
QuestionMenuStrip -> Open new child form Pin
benjamin yap13-May-08 4:31
benjamin yap13-May-08 4:31 
AnswerRe: MenuStrip -> Open new child form Pin
Gareth H13-May-08 4:38
Gareth H13-May-08 4:38 
GeneralRe: MenuStrip -> Open new child form Pin
benjamin yap13-May-08 4:48
benjamin yap13-May-08 4:48 
GeneralRe: MenuStrip -> Open new child form Pin
Gareth H13-May-08 4:52
Gareth H13-May-08 4:52 
GeneralRe: MenuStrip -> Open new child form Pin
benjamin yap13-May-08 5:00
benjamin yap13-May-08 5:00 
AnswerRe: MenuStrip -> Open new child form Pin
darkelv13-May-08 5:13
darkelv13-May-08 5:13 
GeneralRe: MenuStrip -> Open new child form Pin
benjamin yap13-May-08 5:24
benjamin yap13-May-08 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.