How to pass string by value?

How to pass string by value?
glowasAsked:
Who is Participating?
 
smitty22Commented:
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
 
testnCommented:
string will always be passed by value.
0
 
testnCommented:
unless you put a keyword "ref" or "out" in the method declaration
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
smitty22Commented:
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
 
glowasAuthor Commented:
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
 
glowasAuthor Commented:
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
 
Duy PhamFreelance IT ConsultantCommented:
You do not need to create a new string object. Just pass it directly to your function :

YourFunction("YourString");
0
 
testnCommented:
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
 
testnCommented:
What you are doing might be unnecessary then.
0
 
malharoneCommented:
how about just using
string password="dog";
password.Clone   //<-- make a copy and pass by value
0
 
smitty22Commented:
Sorry, I meant:
<computations with s will leave str UNCHANGED, even though s is a direct REFERENCE to str>.

0
 
testnCommented:
smitty22, you're absolutely right. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.