Declaring strings public static readonly versus public const versus public static const

后端 未结 5 1672
故里飘歌
故里飘歌 2021-02-01 02:47

In each project we have there is a file used to store the various SQL statements used in that project. There are a handful of variations on how the class is declared and how the

5条回答
  •  Happy的楠姐
    2021-02-01 03:01

    On const vs static readonly:
    const can have better performance since it's a compiletime constant
    But on the other hand it has problems with binary versioning. The constant gets inlined into the assembly that uses it, so if the assembly that declares it gets changed the other assembly needs to be recompiled or it will use an outdated constant.

    For structs I typically use a static property instead of a readonly field, since the jitter can optimize it, but I still don't have versioning problems.

提交回复
热议问题