?
Solved

References in c#

Posted on 2004-10-08
6
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

777 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