Solved

How to pass string by value?

Posted on 2003-12-12
12
565 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

752 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