C# - 这个声明的字符串是否被视为常量?
帮我解决这里的争论。
这是:
SqlCommand cmd = new SqlCommand( "sql cmd", conn);
与此完全一样对待:
const string s = "sql cmd";
SqlCommand cmd = new SqlCommand( s, conn);
即。 如果我明确指出字符串 s 是 const,会有什么不同吗?
而且,如果不以同样的方式对待它,为什么不呢?
Help me settle an argument here.
Is this:
SqlCommand cmd = new SqlCommand( "sql cmd", conn);
treated exactly the same as this:
const string s = "sql cmd";
SqlCommand cmd = new SqlCommand( s, conn);
Ie. does it make a difference if I state specifically that the string s is a const.
And, if it is not treated in the same way, why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在后面的代码片段中,并不是说字符串是 const,而是变量是 const。 这与 C++ 中的 const 不太一样。 (在 .NET 中,字符串始终是不可变的。)
是的,这两个代码片段执行相同的操作。 唯一的区别是,在第一种形式中,您还将有一个
s
的元数据条目,如果该变量是在类型级别声明的(而不是局部变量),则其他方法可以也用它。 当然,由于字符串驻留,如果您在其他地方使用“sql cmd”,内存中仍然只有一个字符串 object...但是如果您使用反射查看类型,您会发现如果 const 被声明为常量字段,则将 const 作为元数据中的字段与第二个片段一起使用,如果它只是一个局部变量,则在构建一个变量时它将位于 PDB 文件中。In the latter snippet, it's not that the string is const - it's that the variable is const. This is not quite the same as const in C++. (Strings are always immutable in .NET.)
And yes, the two snippets do the same thing. The only difference is that in the first form you'll have a metadata entry for
s
as well, and if the variable is declared at the type level (instead of being a local variable) then other methods could use it too. Of course, due to string interning if you use "sql cmd" elsewhere you'll still only have a single string object in memory... but if you look at the type with reflection you'll find the const as a field in the metadata with the second snippet if it's declared as a constant field, and if it's just a local variable it'll be in the PDB file if you build one.const 的值总是直接烧录到调用者中,所以是的,它们是相同的。
此外,编译器会在源代码中找到字符串 - 如果您多次使用同一个字符串(纯粹从维护角度 - 无论哪种方式结果都是相同的),
const
会很有帮助。The value of a
const
always gets burned directly into the caller, so yes they are identical.Additionally, the compiler interns strings found in source code - a
const
is helpful if you are using the same string multiple times (purely from a maintenance angle - the result is the same either way).SqlCommand 的构造函数不会“看到”任何差异,因此会以相同的方式运行。
the constructor of the SqlCommand will not "see" any difference, and will therefore act the same way.
是的,这些是完全一样的。
Yes, those are exactly the same.
是的,请随意使用 Reflector 来查看程序集,const 字符串将是在编译时替换为文字字符串。 我还有一个 关于此的博客文章可以确保您使用 Reflector 的工作安全:)
Yes, feel free to use Reflector to look at the assembly, const strings will be replaced with literal strings on compilation. I also have a blog post about this to safe you the work of using Reflector :)
我对此不是 100% 确定,但我敢打赌是一样的。
const 只是确保您不会重新评估变量,但这是可以在编译时完成的事情。
此外,字符串是不可变的,所以我认为声明或不声明变量不会有任何区别。
然而,明确的证据是研究这两种情况下生成的 IL 代码。
I'm not 100% sure about this, but I'd bet it is the same.
const will just make sure you don't reassing the variable, but it's something that can be done at compile time.
Morover, strings are inmutable, so I don't think it will make any difference declaring the variable or not.
However, the definite prove would be studing the IL code generated in both cases.