views:

159

answers:

5

How do you pass strings in C Sharp?

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

+4  A: 
SomeFunction("arg1", "arg2");

or as variables:

string arg1 = "some value 1";
string arg2 = "some value 2";
SomeFunction(arg1, arg2);
Darin Dimitrov
+2  A: 

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
}
Amadiere
A: 
string theString = "These are the contents";
SomeOtherFunction(theString);
Hans Kesting
+3  A: 

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;
}
Oliver
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.
KMan
A: 

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.

Asher