为什么 C# 在这种情况下使用相同的内存地址? [英] Why C# using same memory address in this case?

查看:32
本文介绍了为什么 C# 在这种情况下使用相同的内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行这段代码时

class Program
    {
        static void Main(string[] args)
        {
//scope 1 
            {
                string x = "shark";
                string y = x.Substring(0);
                unsafe
                {
                    fixed (char* c = y)
                    {
                        c[4] = 'p';
                    }
                }
                Console.WriteLine(x);
            }
//scope 2
            {
                string x = "shark";
//Why output in this line "sharp" and not "shark" ?
                Console.WriteLine(x);
            }
        }
}

输出为:

sharp
sharp

当我用这样的方法将这 2 个范围分开时:

When I separate this 2 scopes in methods like this:

class Program
    {
        static void Main(string[] args)
        {
            func1();
            func2();
        }
        private static void func2()
        {
            {
                string x = "shark";
                Console.WriteLine(x);
            }
        }

        private static void func1()
        {
            {
                string x = "shark";
                string y = x.Substring(0);
                unsafe
                {
                    fixed (char* c = y)
                    {
                        c[4] = 'p';
                    }
                }
                Console.WriteLine(x);
            }
        }
    }

输出为:

sharp
shark

已编辑

我也是这样试的:

  class Program
    {
        static void Main(string[] args)
        {
            {
                string x = "shark";
                string y = x.Substring(0);
                unsafe
                {
                    fixed (char* c = y)
                    {
                        c[4] = 'p';
                    }
                }
                Console.WriteLine(x);
            }
            void Test(){
                {
                    string x = "shark";
                    Console.WriteLine(x);
                }
            }
            Test();
        }
}

输出为:

 sharp
 shark

**我使用的环境是 MacOS 和 .net core 2.2 (Rider) **

**Environment which I used is MacOS and .net core 2.2 (Rider) **

我希望在所有情况下都有相同的输出,但输出不同.正如我们所知,实习是将您硬编码的所有字符串放入汇编中并在整个应用程序中全局重用以重用相同的内存空间.但是在这段代码的情况下,我们看到

I expect to have same output in all cases, but the output is different. As we know interning is that all strings you hardcoded are put into assembly and reused globally in the whole application to reuse same memory space. But in case of this code we see that

硬编码字符串仅在函数范围内重用,不在全局范围内.

这是 .NET Core 错误还是有解释?

推荐答案

一个函数一个函数的编译,这就是为什么在第一种情况下,如果你把字符串放在一个范围内编译器一次检查字符串并放入 Intern Pool 只有一个字符串,当您在内存中更改时,所有硬编码字符串都会更改.

Compilation going function by function thats why in first case if you put string in one scope compiler checking strings one time and putting in Intern Pool only one string and when you change in memory it's changed for all hardcoded strings.

在第二种情况下,当我将字符串放在不同的函数中时,编译器将字符串添加到 Intern Pool 仅用于第一个函数,并且当我们使用不安全代码更改字符串时,它在编译器转到第二种方法后对该字符串应用更改并试图找到字符串 shark 但在 Interned Pool 编译器找不到那种字符串(因为我们已经将其更改为 sharp),这就是为什么它添加新字符串并且输出变得不同的原因.

In second case when I put strings in different functions compiler adding string to Intern Pool for only first function and when we changing string with unsafe code its applying changes to that string after compiler going to second method and trying to find the string shark but in Interned Pool compiler could not find that kind of string (because we already change it to sharp), thats why it add new string and the output become different.

这篇关于为什么 C# 在这种情况下使用相同的内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆