473,549 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ 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.WriteLi ne("String before function : " + myString);
doSomethingToSt ring(myString);
Console.WriteLi ne("String afer function : " + myString);

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

Oct 31 '06 #1
4 1553
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.WriteLi ne("String before function : " + myString);
doSomethingToSt ring(ref myString);
Console.WriteLi ne("String afer function : " + myString);

static void doSomethingToSt ring(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.WriteLi ne("String before function : " + myString);
doSomethingToSt ring(myString);
Console.WriteLi ne("String afer function : " + myString);

static void doSomethingToSt ring(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********@gmai l.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
9814
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 must be an object instead of
4
3395
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any...
13
2767
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
1529
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 an assembly A which refers to class Y in assembly B. So I add a reference in assembly A to assembly B, and attempt to build. I get an error message,...
5
10315
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 "Add Web Reference" for the same service the same function works appropriately. Here is the client proxy generated from WSDL.exe ...
12
2668
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
1882
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
3101
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 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
1
1829
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 "below" is just a reference. But, what if
275
12102
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
7524
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3501
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.