Solved

References in c#

Posted on 2004-10-08
6
173 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
Comment Utility
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
Comment Utility
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
Comment Utility
string is a very special case which behaves like a value type
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lcoolsingh
Comment Utility
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
Comment Utility
try  the way i suggested
0
 

Author Comment

by:lcoolsingh
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now