Solved

How to pass string by value?

Posted on 2003-12-12
12
560 Views
Last Modified: 2010-04-16
How to pass string by value?
0
Comment
Question by:glowas
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now