首页 新闻 会员 周边

const 的地址?

0
悬赏园豆:5 [已解决问题] 解决于 2012-08-30 23:24

c#  const string s="hello"

为什么,随着程序的单步调试,s在内存中的地址指向一直在变化,什么原因,想知道内部是如何分配地址的??????????、

奔*跑的主页 奔*跑 | 初学一级 | 园豆:72
提问于:2012-05-26 22:59
< >
分享
最佳答案
0

const类型的对象不是变量,是常量,以下语句等同:

 

const string s="hello";

string s1 = s;

 

string s1="hello";

 

当你在语句中使用到const对象的时候,编译系统是直接把const对象定义的内容复制到使用这个对象的地方。

 

一下练习你会了解const的特点(与static readonly对象比较):

Dll1:

namespace Dll1

{

public class Data

{

    public readonly static string s1="version 1 hello1";

    public const string s2="version 1 hello2";

}

}

 

Dll2:

namespace Dll2

{

class App

{

public string GetDll1S1()

{

    return Dll1.Data.s1;

}

public string GetDll1S2()

{

    return Dll1.Data.s2;

}

}

}

 

以上代码存在于两个模块中。当你因为某种原因(比如DLL1是由第三方提供的)修改了DLL1时,比如把内容修改为:

namespace Dll1

{

public class Data

{

    public readonly static string s1="version 2 hello1";

    public const string s2="version 2 hello2";

}

}

 

此时,如果你不重新编译DLL2,你会发现DLL2中两个函数返回结果的不同。

收获园豆:3
无之无 | 大侠五级 |园豆:5095 | 2012-05-27 01:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册