pass by reference?

Posted on 2006-04-11
Last Modified: 2010-04-16
How do I call GetClientDSN(ref string)  ?

How do I pass in my parameter by reference in C#?
Question by:knowlton
    LVL 3

    Accepted Solution

    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);
    LVL 12

    Assisted Solution

    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"


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now