1

How do you pass strings in C#?

How do you pass string variables as arguments to a method/procedure/function in a program written in C#?

2
  • 1
    Do you mean how to pass literal strings? Commented Apr 1, 2010 at 7:34
  • At last, a question I can answer! But, drat, I've been beaten to it!!! Commented Apr 1, 2010 at 8:14

5 Answers 5

5
SomeFunction("arg1", "arg2"); 

or as variables:

string arg1 = "some value 1"; string arg2 = "some value 2"; SomeFunction(arg1, arg2); 
Sign up to request clarification or add additional context in comments.

Comments

5

The string class is immutable. So you can't do the following:

private void MyFunction() { string myMessage = "Just a message"; ManipulateMessage(myMessage); Console.WriteLine(myMessage); } private void ManipulateMessage(string message) { message = DateTime.Now + " " + message; } 

To get this to work you have to pass back the string:

private void MyFunction() { string myMessage = "Just a message"; myMessage = ManipulateMessage(myMessage); Console.WriteLine(myMessage); } private string ManipulateMessage(string message) { return DateTime.Now + " " + message; } 

Or Use a StringBuilder

private void MyFunction() { StringBuilder myMessage = "Just a message"; ManipulateMessage(myMessage); Console.WriteLine(myMessage.ToString()); } private void ManipulateMessage(StringBuilder message) { message.Insert(0, DateTime.Now + " "); } 

Update after comment from KMan

Ok, there is a third version using the ref keyword

private void MyFunction() { string myMessage = "Just a message"; ManipulateMessage(ref myMessage); Console.WriteLine(myMessage); } private void ManipulateMessage(ref string message) { message = DateTime.Now + " " + message; } 

1 Comment

You can also use the ref keyword like: private void ManipulateMessageEx(ref string message) { message = DateTime.Now + "-" + message; } and use it like: string myMessage = "Just a message";ManipulateMessageEx(ref myMessage); MessageBox.Show(myMessage); to update the string.
2

You mean a method like:

public bool SomeMethod(string inputString) { // do stuff return true; } 

Then call like:

string testString = "Here is some text"; if (SomeMethod(testString)) { // do stuff } 

Comments

0
string theString = "These are the contents"; SomeOtherFunction(theString); 

Comments

0

Just curious if you are asking this question because of string handling in other languages like Delphi?

Strings in C# are immutable (as others have said) so any change to a string allocates memory for a "new" one and the "old" one will eventually be garbage collected. This means that the compiler won't generate code to make sure that the reference count is decremented when the method returns - or any of that cool stuff.

You can also pass it by reference (see example 6)

... string myLittleString = "something"; PassToMe(ref myLittleString); ... void PassToMe(ref string takenIn) { //some code here }

but this won't make much difference if you are going to change the string inside the method (since strings are immutable). If you plan to make lots of changes to the passed string, it is better to use a StringBuilder IMO.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.