C# string reference type?

前端 未结 10 1202
梦如初夏
梦如初夏 2020-11-22 10:10

I know that \"string\" in C# is a reference type. This is on MSDN. However, this code doesn\'t work as it should then:

class Test
{
    public static void          


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 10:32

    Actually it would have been the same for any object for that matter i.e. being a reference type and passing by reference are 2 different things in c#.

    This would work, but that applies regardless of the type:

    public static void TestI(ref string test)
    

    Also about string being a reference type, its also a special one. Its designed to be immutable, so all of its methods won't modify the instance (they return a new one). It also has some extra things in it for performance.

提交回复
热议问题