473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reference type

i understand that in csharp "string" and "object" are reference type...

For e.g. in this case
myString is passed to a method and it changes the value of the string..
if string is a reference type, isnt it passed to the method by
reference and the second WriteLine should be "edited" instead of "hello
world" ?

String myString = "hello world";

Console.WriteLine("String before function : " + myString);
doSomethingToString(myString);
Console.WriteLine("String afer function : " + myString);

static void doSomethingToString(string myString)
{
myString = "edited";
}

Oct 31 '06 #1
4 1544
gpg
In C# arguments are passed by value - thus only the copy myString in
function scope is modified.

If you want to modify an argument, you must use the 'ref' keyword to
pass the argument by reference.

For example
>
String myString = "hello world";

Console.WriteLine("String before function : " + myString);
doSomethingToString(ref myString);
Console.WriteLine("String afer function : " + myString);

static void doSomethingToString(ref string myString)
{
myString = "edited";
}
will result in myString being modified in the main scope.

GPG

Oct 31 '06 #2
It is a reference type passed by value; since you reassign the string this
is behaving correctly.

As always, Jon puts it better than I could:
http://www.yoda.arachsys.com/csharp/parameters.html

(sorry Jon, I always intend to use the pobox address, but google says
yoda...)

Marc
Oct 31 '06 #3
i understand that in csharp "string" and "object" are reference type...
>
For e.g. in this case
myString is passed to a method and it changes the value of the string..
if string is a reference type, isnt it passed to the method by
reference and the second WriteLine should be "edited" instead of "hello
world" ?

String myString = "hello world";

Console.WriteLine("String before function : " + myString);
doSomethingToString(myString);
Console.WriteLine("String afer function : " + myString);

static void doSomethingToString(string myString)
{
myString = "edited";
}
No, because with the statement
myString = "edited";
you don't change the contents of the original string, but create a new
string (containing "edited") and changing the reference of the local
variable myString to point to that new string.
When you return from that function, the original reference is still in
place, so you see the original text. (Note: there are two separate
'myString' variables here: one 'global', the other local to the method)

Hans Kesting
Oct 31 '06 #4
vi********@gmail.com wrote:
i understand that in csharp "string" and "object" are reference type...

For e.g. in this case
myString is passed to a method and it changes the value of the string..
No, actually it doesn't change the value of the string. Strings are
immutable in .NET, meaning that the value of the string object never
changes.

Instead a completely new string object is used and the reference to that
object replaces the old reference.
if string is a reference type, isnt it passed to the method by
reference and the second WriteLine should be "edited" instead of "hello
world" ?
No, all parameters are passed by value by default. For a reference type
that means that the reference is passed by value, i.e. the value of the
reference is copied.

In the method you are using the copy of the reference, so eventhough you
replace the reference with the reference to the new string, that does
not change the original reference.

Oct 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
9806
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
3391
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
13
2747
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
4
1520
by: Edward Diener | last post by:
I have a class Y in assembly B which is derived from a class Z in assembly C. So I correctly add a reference to assembly C in assembly B, build assembly B and everything builds fine. Now I create...
5
10303
by: Mike Logan | last post by:
I used WSDL.exe to generate a client side web proxy for a web service, called the web service, got the results but an array returned by the web service is not in the results. However if I use...
12
2661
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
9
1876
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
27
3091
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
1
1825
by: az.anonymous | last post by:
Im starting to learn C#, and I made a simple stack class. Inside the stack class I had the following: class StackElement { object info; StackElement below; } Everything works fine cause...
275
12024
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.