Home > Backend Development > C++ > Why Does C# String Behavior Seem to Contradict its Reference Type Classification?

Why Does C# String Behavior Seem to Contradict its Reference Type Classification?

Linda Hamilton
Release: 2025-01-24 07:16:09
Original
412 people have browsed it

Why Does C# String Behavior Seem to Contradict its Reference Type Classification?

C# string "reference type" exception

The C# documentation states that strings are reference types. However, the code below exhibits confusing behavior:

<code class="language-csharp">class Test
{
    public static void Main()
    {
        string test = "before passing";
        Console.WriteLine(test);
        TestI(test);
        Console.WriteLine(test);
    }

    public static void TestI(string test)
    {
        test = "after passing";
    }
}</code>
Copy after login

According to the definition of the reference type, the output should be "before passing" and "after passing", which indicates that the string was modified in the TestI method. However, the actual output is "before passing" and "before passing", which indicates that the string is passed by value rather than by reference.

To understand this exception, we need to delve into the subtleties of reference types. Although strings are technically reference types, the references themselves are passed by value. Passing a reference by value means that the variable containing the reference is copied, not the object itself. Therefore, any changes to the reference inside the method will not affect the original string outside the method.

To correct this and allow the string to be modified, we need to pass the reference by reference using the "ref" keyword:

<code class="language-csharp">public static void TestI(ref string test)</code>
Copy after login

Any changes to the reference inside the TestI method will now be reflected in the original string outside the method.

It is important to note the difference between changing the object a variable refers to and changing the reference itself. Strings are immutable, which means their contents cannot be modified. However, we can reassign the reference variable to point to a different string, as shown in the following StringBuilder example:

<code class="language-csharp">using System.Text;

class Test
{
    public static void Main()
    {
        StringBuilder test = new StringBuilder();
        Console.WriteLine(test);
        TestI(test);
        Console.WriteLine(test);
    }

    public static void TestI(StringBuilder test)
    {
        test.Append("changing");
    }
}</code>
Copy after login

In this example, instead of changing the value of the "test" parameter, we change the data in the object it refers to.

The above is the detailed content of Why Does C# String Behavior Seem to Contradict its Reference Type Classification?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template