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 5958839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:36:01+00:00 2026-05-22T18:36:01+00:00

The string type in C# is a reference type, and passing a reference type

  • 0

The string type in C# is a reference type, and passing a reference type argument by value copies the reference so that I don’t need to use the ref modifier. However, I need to use the ref modifier for modifying the input string. Why is this?

using System;

class TestIt
{
    static void Function(ref string input)
    {
        input = "modified";
    }

    static void Function2(int[] val) // Don't need ref for reference type
    {
        val[0] = 100;
    }

    static void Main()
    {
        string input = "original";
        Console.WriteLine(input);
        Function(ref input);      // Need ref to modify the input
        Console.WriteLine(input);

        int[] val = new int[10];
        val[0] = 1;
        Function2(val);
        Console.WriteLine(val[0]);
    }
}
  • 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-05-22T18:36:02+00:00Added an answer on May 22, 2026 at 6:36 pm

    The reason you need to ref the string parameter is that even though you pass in a reference to a string object, assigning something else to the parameter will just replace the reference currently stored in the parameter variable. In other words, you have changed what the parameter refers to, but the original object is unchanged.

    When you ref the parameter, you have told the function that the parameter is actually an alias for the passed-in variable, so assigning to it will have the desired effect.

    EDIT: Note that while string is an immutable reference type, that’s not too relevant here. Since you’re just trying to assign a new object (in this case the string object “modified”), your approach wouldn’t work with any reference type. For example, consider this slight modification to your code:

    using System;
    
    class TestIt 
    {
        static void Function(ref string input)
        {
            input = "modified";
        }
    
        static void Function2(int[] val) // don't need ref for reference type
        {
            val = new int[10];  // Change: create and assign a new array to the parameter variable
            val[0] = 100;
        }
        static void Main()
        {
            string input = "original";
            Console.WriteLine(input);
            Function(ref input);      // need ref to modify the input
            Console.WriteLine(input);
    
            int[] val = new int[10];
            val[0] = 1;
            Function2(val);
            Console.WriteLine(val[0]); // This line still prints 1, not 100!
        }
    }
    

    Now, the array test “fails”, because you’re assigning a new object to the non-ref parameter variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Delphi 2009 has changed its string type to use 2 bytes to represent a
I need to convert from a SQLVARCHAR to a string data type Variable definitions
I am in need of a case insensitive string enumeration type in my XML
In .Net you can read a string value into another data type using either
Type.GetType(System.String) Is there a lookup for the aliases available somewhere? Type.GetType(string) returns null .
When I pass an immutable type object(String, Integer,.. ) as final to a method
Given: FieldInfo field = <some valid string field on type T>; ParameterExpression targetExp =
I think the field type should be string of variable length (VARCHAR), but what
This is my code: type HoraAtendimento = (String, Int, Int) htmlHAtendimento :: [HoraAtendimento] ->
How can you obtain the Type (the name as a string is sufficient) of

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.