Solved

References in c#

Posted on 2004-10-08
6
175 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Keyboard 2 33
Hey!! 5 33
RegEx with optional part 4 24
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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