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

查看:79
本文介绍了在这种情况下,为什么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

当我使用以下方法将这两个范围分开时:

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

硬编码的字符串仅在函数范围内而不在全局范围内重复使用。

hardcoded strings reused only in function scope and not in global scope.

这是.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 中我们使用不安全的代码更改了字符串,然后在编译器转到第二种方法并尝试在 Interned Pool 中找到字符串 shark 后将其应用于该字符串。 code>编译器找不到这种字符串(因为我们已经将其更改为 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天全站免登陆