且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在这种情况下,为什么C#使用相同的内存地址?

更新时间:2023-09-16 18:31:22

编译功能通过函数,这就是为什么在第一种情况下,如果将字符串放入一个范围编译器一次检查字符串,然后仅将 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.