Solved

References in c#

Posted on 2004-10-08
6
176 Views
Last Modified: 2010-04-15
Hi there,

Im new to c#, and am not understanding 1 aspect of references. Take the following example:
   
        public  class CRunMe{

          public static void Main(){

             string s = "Default val";
             
              CRunMe.ChangeMe(s);
           
              Console.Writeln(s);   // Should show the new value, but it doesnt..
           
          }
         
          public static void ChangeMe(string str){
             
             str = "New value assigned";
     
          }
        }

Now according to me, a string type is a reference type, so in the above example, afer returnining from changeme, the string variable 's' should be changed 2 reflect the new value, but it doesnt... why is this.. surely a reference type like the above should be changed? In c++, whenever you pass something as a pointer or a reference parameter, if your intention is to modify the passed in parameter, then this will happen. However if i use the ref keyword, then this change takes place..

Thanks


0
Comment
Question by:lcoolsingh
6 Comments
 
LVL 3

Expert Comment

by:skpatra
ID: 12256977
By default  all primitive datatypes are passed by value and all objects are passed by reference in c#. "string" is identified as a primitive data type. However, all primitive datatypes can be 'boxed' to object types. The corrsponding object type for the datatype string is String. If you pass a String (not a string, mark the difference in case) as an argument it will be passed by reference.
0
 
LVL 18

Accepted Solution

by:
armoghan earned 50 total points
ID: 12257077
if you want to pass by ref do this

 public  class CRunMe{

          public static void Main(){

             string s = "Default val";
             
              CRunMe.ChangeMe(ref s);
           
              Console.WriteLine(s);   // Should show the new value, but it doesnt..
           
          }
         
          public static void ChangeMe(ref string str){
             
             str = "New value assigned";
     
          }
        }
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12257177
string is a very special case which behaves like a value type
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:lcoolsingh
ID: 12286148
I tried using the uppercase System.String, and passed this into the above function, but unfortunately this did not work either
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12294920
try  the way i suggested
0
 

Author Comment

by:lcoolsingh
ID: 12298232
I used your way, and i know that by using ref or out, the intended modification takes place.. but as the string type is kind of a reference type, i thought this would happen naturally...

Thanks all
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question