Solved

How to pass string by value?

Posted on 2003-12-12
12
563 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 57
Why does my excel data start at B1 instead of A1. 9 83
Hey!!!! 1 20
What Does This C# Code Block Do? 5 30
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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