Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:59:54+00:00 2026-06-04T13:59:54+00:00

When I pass a string to a function, is a pointer to the string’s

  • 0

When I pass a string to a function, is a pointer to the string’s contents passed, or is the entire string passed to the function on the stack like a struct would be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:59:55+00:00Added an answer on June 4, 2026 at 1:59 pm

    A reference is passed; however, it’s not technically passed by reference. This is a subtle, but very important distinction. Consider the following code:

    void DoSomething(string strLocal)
    {
        strLocal = "local";
    }
    void Main()
    {
        string strMain = "main";
        DoSomething(strMain);
        Console.WriteLine(strMain); // What gets printed?
    }
    

    There are three things you need to know to understand what happens here:

    1. Strings are reference types in C#.
    2. They are also immutable, so any time you do something that looks like you’re changing the string, you aren’t. A completely new string gets created, the reference is pointed at it, and the old one gets thrown away.
    3. Even though strings are reference types, strMain isn’t passed by reference. It’s a reference type, but the reference itself is passed by value. Any time you pass a parameter without the ref keyword (not counting out parameters), you’ve passed something by value.

    So that must mean you’re…passing a reference by value. Since it’s a reference type, only the reference was copied onto the stack. But what does that mean?

    Passing reference types by value: You’re already doing it

    C# variables are either reference types or value types. C# parameters are either passed by reference or passed by value. Terminology is a problem here; these sound like the same thing, but they’re not.

    If you pass a parameter of ANY type, and you don’t use the ref keyword, then you’ve passed it by value. If you’ve passed it by value, what you really passed was a copy. But if the parameter was a reference type, then the thing you copied was the reference, not whatever it was pointing at.

    Here’s the first line of the Main method:

    string strMain = "main";
    

    We’ve created two things on this line: a string with the value main stored off in memory somewhere, and a reference variable called strMain pointing to it.

    DoSomething(strMain);
    

    Now we pass that reference to DoSomething. We’ve passed it by value, so that means we made a copy. It’s a reference type, so that means we copied the reference, not the string itself. Now we have two references that each point to the same value in memory.

    Inside the callee

    Here’s the top of the DoSomething method:

    void DoSomething(string strLocal)
    

    No ref keyword, so strLocal and strMain are two different references pointing at the same value. If we reassign strLocal…

    strLocal = "local";   
    

    …we haven’t changed the stored value; we took the reference called strLocal and aimed it at a brand new string. What happens to strMain when we do that? Nothing. It’s still pointing at the old string.

    string strMain = "main";    // Store a string, create a reference to it
    DoSomething(strMain);       // Reference gets copied, copy gets re-pointed
    Console.WriteLine(strMain); // The original string is still "main" 
    

    Immutability

    Let’s change the scenario for a second. Imagine we aren’t working with strings, but some mutable reference type, like a class you’ve created.

    class MutableThing
    {
        public int ChangeMe { get; set; }
    }
    

    If you follow the reference objLocal to the object it points to, you can change its properties:

    void DoSomething(MutableThing objLocal)
    {
         objLocal.ChangeMe = 0;
    } 
    

    There’s still only one MutableThing in memory, and both the copied reference and the original reference still point to it. The properties of the MutableThing itself have changed:

    void Main()
    {
        var objMain = new MutableThing();
        objMain.ChangeMe = 5; 
        Console.WriteLine(objMain.ChangeMe); // it's 5 on objMain
    
        DoSomething(objMain);                // now it's 0 on objLocal
        Console.WriteLine(objMain.ChangeMe); // it's also 0 on objMain   
    }
    

    Ah, but strings are immutable! There’s no ChangeMe property to set. You can’t do strLocal[3] = 'H' in C# like you could with a C-style char array; you have to construct a whole new string instead. The only way to change strLocal is to point the reference at another string, and that means nothing you do to strLocal can affect strMain. The value is immutable, and the reference is a copy.

    Passing a reference by reference

    To prove there’s a difference, here’s what happens when you pass a reference by reference:

    void DoSomethingByReference(ref string strLocal)
    {
        strLocal = "local";
    }
    void Main()
    {
        string strMain = "main";
        DoSomethingByReference(ref strMain);
        Console.WriteLine(strMain);          // Prints "local"
    }
    

    This time, the string in Main really does get changed because you passed the reference without copying it on the stack.

    So even though strings are reference types, passing them by value means whatever goes on in the callee won’t affect the string in the caller. But since they are reference types, you don’t have to copy the entire string in memory when you want to pass it around.

    Further resources:

    • Here is the best article I’ve read on the difference between reference types and value types in C#, and why a reference type isn’t the same as a reference-passed parameter.
    • As usual, Eric Lippert also has several excellent blog posts on the subject.
    • He has some great stuff on immutability, too.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to pass the pointer of a std::string to a function which
I have a function like below void functionA(unordered_map<string, classA*>* arg1); I need to pass
I need to pass a string literal to a function myfunction(arg1 DEF_CHAR arg1); now
Trying to pass a string argument to a function, which will then be used
I'm trying to pass a string argument to a target function in a process.
i'm trying to pass a string from main to another function. this string is
I want to be able to pass a string (['parent_array']['child_array']) to a function that
A function takes a string as argument, example fn(string). I want to pass different
Let's say I have a function like: void myFunc(List<AClass> theList) { string[] stuff =
I'm attempting to pass a pointer to a function that is defined in one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.