Solved

How to pass string by value?

Posted on 2003-12-12
12
564 Views
Last Modified: 2010-04-16
How to pass string by value?
0
Comment
Question by:glowas
[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
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 18

Expert Comment

by:testn
ID: 9929398
string will always be passed by value.
0
 
LVL 18

Expert Comment

by:testn
ID: 9929404
unless you put a keyword "ref" or "out" in the method declaration
0
 
LVL 2

Expert Comment

by:smitty22
ID: 9929836
Actually, Strings are pass by reference in C#, just the same as other objects.  However, Strings have the nature of pass by value by virtue of the fact that String objects are immutable.  In other words, you can't modify the value of an instance of String.

There are no String method that modify a string that do not return a _new_ instance of the string class.  Because of this, you achieve pass-by-value semantics without actually implementing a pass-by-value mechanism.

Note, this argument _only_ works for Strings.

0
Independent Software Vendors: 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:glowas
ID: 9929925
There are 2 types of strings in C#

string and String

In the books they say strings are passed by reference by default because they classes allocated on heaps.
I don' acctually understand what smitty22 is saying. Are you saying that even they are passed by reference, they are passed by value???

Can you please explain and maybe give an example...
0
 

Author Comment

by:glowas
ID: 9929956
I give you example what I am doing to have them passed by value:

I want to pass string text by value to MyFunction.
string text="MyString";
string pass_string= new string(text.ToCharArray());
MyFunction(pass_string);

Is what I am doing unnecessary, for example in multi thread application?

Ps. I doubled points for that question since I ask for more explanation.
0
 
LVL 10

Expert Comment

by:Duy Pham
ID: 9930107
You do not need to create a new string object. Just pass it directly to your function :

YourFunction("YourString");
0
 
LVL 18

Expert Comment

by:testn
ID: 9930150
string is just an alias for String. It's actually the same. It is passed by value. You can try to change value in the called function. It will not affect the value in the calling function.
0
 
LVL 18

Expert Comment

by:testn
ID: 9930158
What you are doing might be unnecessary then.
0
 
LVL 9

Expert Comment

by:malharone
ID: 9930340
how about just using
string password="dog";
password.Clone   //<-- make a copy and pass by value
0
 
LVL 2

Accepted Solution

by:
smitty22 earned 250 total points
ID: 9930426
glowas, my point was essentially that you don't need to worry about it.  Strings/strings ARE passed by reference, but because of the nature of immutability they assume the characteristics of being passed by value.

The main reason one passes by value is so that the original value remains unchanged by any computations performed on the parameter.  Since strings _cannot_ be changed, there is no risk of changing the original value.

Consider the following example:

////  within some other method
  string str = "This is a string.";  
  Example( str );
////

public void Example( string s ) {
  <computations with s will leave str UNCHANGED>
}


Again, my point is that you don't have to worry about it.
0
 
LVL 2

Expert Comment

by:smitty22
ID: 9930444
Sorry, I meant:
<computations with s will leave str UNCHANGED, even though s is a direct REFERENCE to str>.

0
 
LVL 18

Expert Comment

by:testn
ID: 9931930
smitty22, you're absolutely right. :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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