readonly 和 const 有啥区别?

lyshrine 2014-01-21 09:53:39
public static readonly string 照片相似度低 = "";
const string 照片相似度低 = "";

是说 readonly 可以赋值,const 不能赋值吗?
...全文
282 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
游离失所 2014-01-21
  • 打赏
  • 举报
回复
看到楼主的问题,我也去搜了下。。而且自身实际体会了下。。它们分别有2点不同 1.const只能对值类型进行初始化赋值的操作,对于引用类型(string除外),初始化时只能赋值为NULL。。而readonly没这些限制 2.正如2楼所说的。。const是在编译时就已经解析了,然后对常量赋值了。。而readonly是在执行的时候才对常量进行赋值。。可能这么说不太容易明白,我刚看资料时在网上看到一段代码,贴你看看。。

using System;

class P
{

    static readonly int A=B*10;

    static readonly int B=10;   

    public static void Main(string[] args)

    {

        Console.WriteLine("A is {0},B is {1} ",A,B);

    }

}

using System;
class P
{
    const int A=B*10;
    const int B=10;   
    public static void Main(string[] args)
    {
        Console.WriteLine("A is {0},B is {1} ",A,B);
    }
}
声明readonly输出的是0和10 声明const输出的是10和100 但如果把readonly中声明A和B的位置换一下。。输出的结果也是10和100。。 看到这里应该明白了吧?
wanghui0380 2014-01-21
  • 打赏
  • 举报
回复
最简单的例子 const int const_a=const_b*10 const int const_b=10; 因为编译器在编译时候已经确定好了值了,所以const_a是10*10=100 readonly int a=b*10; readonly int b=10 这个运行时候才确定值,虽然你b=10,但还没运行到这里,所以b为默认滴0,此时a的值为0*10=0
zlbcdn 2014-01-21
  • 打赏
  • 举报
回复
引用 2 楼 bdmh 的回复:
readonly是运行时检查 const是编译时检查
赞一个!
md5e 2014-01-21
  • 打赏
  • 举报
回复
引用 4 楼 wddw1986 的回复:
[quote=引用 3 楼 liuchaolin 的回复:] private static string _abc = ""; public static readonly string abc { get { return _abc; } } ====================> const string abc 这样呢?应该直接点了吧
你整一堆不能编译的代码发出来是什么意思?[/quote] 那再举个例子吧 /// <summary> /// NLPIR.dll所在的相对于当前页面的位置 /// </summary> const string path = @"../NLPIR/bin/NLPIR.dll";//设定dll的路径 readonly string path2 = @"../NLPIR/bin/NLPIR.dll"; //对函数进行申明 [DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_Init")] public static extern bool NLPIR_Init(String sInitDirPath, int encoding); //特别注意,C语言的函数NLPIR_API const char * NLPIR_ParagraphProcess(const char *sParagraph,int bPOStagged=1);必须对应下面的申明 [DllImport(path, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Winapi, EntryPoint = "NLPIR_ParagraphProcess")] 有些东西是须要用常量才行的,红色部份用path2肯定会报错
youzelin 2014-01-21
  • 打赏
  • 举报
回复
const 定义的常量经过编译后,在编译后的 DLL 当中已经不存在,直接被替换成数字或者字符串。优点是性能高。缺点是只能是基本类型数据。 readonly 定义的静态变量经过编译后,在 DLL 中依然是变量。优点是如果 A.exe 引用 B.dll 的 readonly 变量 C,当 C 的值发生变化,重新编译 B.dll 即可更新 C 的值,不必重新编译 A.exe。
cheng2005 2014-01-21
  • 打赏
  • 举报
回复
引用 3 楼 liuchaolin 的回复:
private static string _abc = ""; public static readonly string abc { get { return _abc; } } ====================> const string abc 这样呢?应该直接点了吧
你整一堆不能编译的代码发出来是什么意思?
md5e 2014-01-21
  • 打赏
  • 举报
回复
private static string _abc = ""; public static readonly string abc { get { return _abc; } } ====================> const string abc 这样呢?应该直接点了吧
bdmh 2014-01-21
  • 打赏
  • 举报
回复
readonly是运行时检查 const是编译时检查
种草德鲁伊 2014-01-21
  • 打赏
  • 举报
回复
const声明的是常量,其他的是变量。
yujian199005 2014-01-21
  • 打赏
  • 举报
回复
引用 2 楼 bdmh 的回复:
readonly是运行时检查 const是编译时检查
学习了!
lyshrine 2014-01-21
  • 打赏
  • 举报
回复
多谢各位了:)

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧