Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Passing arguments by reference

Passing arguments by reference

Passing arguments by reference

Correct me if I'm wrong: in the signature/call like this

CODE --> C#

SomeFunction(int iArg1, string cStr1, ref int iArg2, string cStr2) 

the both (or any, for that matter) arguments after the "ref" are passed by reference - correct?


On the footnote: I can't recall, neither find anywhere, where I've seen this rule, that any argument after "ref" is passed by reference... or I'm mixing it up with "Optional" in VB? Acute case of CRS. sad
"With aging, eyesight is 2nd thing to go, and I can't recall what the 1st one is..." smile



RE: Passing arguments by reference

If you want your function (SomeFunction) to modify the value of a parameter, each parameter you want to modify needs to have the ref qualifier.

In your example, you can modify the values for iArg2 and cStr2 within the function, but once the function is returned to the caller, cStr2 will have it's original value. I tested this with:


static void Main(string[] args)
            int iArg1 = 0;
            string cStr1 = "Red";
            int iArg2 = 1;
            string cStr2 = "Green";

            SomeFunction(iArg1, cStr1, ref iArg2, cStr2);
            Console.WriteLine("iArg2: " + iArg2.ToString());
            Console.WriteLine("cStr2: " + cStr2);

        private static void SomeFunction(int iArg1, string cStr1, ref int iArg2, string cStr2)
            iArg2 = 3;
            cStr2 = "Four";

cStr2 does not have the ref qualifier. Inside the function, it's value is changed to "Four", but in the Console.WriteLine, you will see that it's value is "Green".

Microsoft SQL Server MVP
My Blogs
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Passing arguments by reference

Mix it with "Optional" in VB did I (Sorry, Master yoda! )


Like I've said: acute case of CRS! blush
Thank you, colleague!



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close