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

Why Does C# String Behavior Seem to Contradict Its Reference Type Nature?

Linda Hamilton
Release: 2025-01-24 06:56:10
Original
659 people have browsed it

Why Does C# String Behavior Seem to Contradict Its Reference Type Nature?

Why don’t C# strings work like reference types in some cases?

Although string is defined as a reference type in C#, it behaves differently in certain code scenarios. Consider the following code:

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

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

The expected output of this code is "before passing" and "after passing", reflecting the reference nature of strings. However, the actual output is that both lines are "pass before", indicating that it is passed by value rather than by reference.

The reason for this difference is the way references are handled in C#. The string type is indeed a reference type, but when a reference is passed as a parameter to a method, it is passed by value. This means that a copy of the reference is created rather than a reference to the original value.

If you want to pass a string by reference, you must use the "ref" keyword:

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

This ensures that the method receives a reference to the original string, allowing modifications made within the method to be reflected in the calling code.

It is important to distinguish between changing the value of a referenced object and changing the reference itself. Strings are immutable and cannot be modified, but references can be reassigned to different strings.

To demonstrate more clearly, let's use a different example involving a mutable type, StringBuilder:

<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("修改");
    }
}</code>
Copy after login

In this case, the output will be "" and "modified", which indicates that modifications made in the method are reflected in the calling code. This is because StringBuilder is a mutable reference type.

The above is the detailed content of Why Does C# String Behavior Seem to Contradict Its Reference Type Nature?. 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