pass by reference?

How do I call GetClientDSN(ref string)  ?

How do I pass in my parameter by reference in C#?
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

string is automatically passed by reference in c# and so are the objects.. but if you need to pass any explicitly by ref, just use the keyword ref in the parameter declaration..

private bool test(ref int a)


int b = 0;
bool check = this.test(ref b);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This is something worth spending some time on to get your understanding right. Basically there are two Types ( of variables: value-types  and reference-types. You can pass variables as arguments by Value or by Reference. But Value and Reference Types should not be confused with passing by value or reference.

A string is in fact a reference type. Quoting from MSDN (why say it again if it has been said properly already):
"A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref (or out) keyword. For simplicity, the following examples use ref."

So, to pass a string to a method, and have any changes persist outside the method, you need to pass it by reference:
public void GetClientDSN(ref string myString)
    myString = "b";

And to call it...
public void TestCall()
    string testString = "a";
    GetClientDSN(ref testString); // <-------------NOTE the "ref" keyword here as well
    Console.WriteLine(testString); // writes "b"

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.