Nef*_*ion 4 c# string parameters const function-calls
我有一个像这样的小功能:
void Bar(string s)
{
*/ do somthing with the string here.*/
}
void Foo()
{
Bar("Hello");
}
Run Code Online (Sandbox Code Playgroud)
如果我看一下IL输出,它给了我以下内容:
.method private hidebysig instance void Foo() cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: ldstr "Hello"
L_0006: call instance void TestApp.MainWindow::Bar(string)
L_000b: ret
}
Run Code Online (Sandbox Code Playgroud)
现在我以为我会替换它的const string字段.
const string str= "Hello";
void Foo()
{
Bar(str);
}
Run Code Online (Sandbox Code Playgroud)
转换为完全相同的 IL片段.
现在我的问题是使用哪一个?
Foo("Hello"); 要么 Foo(cHello);
谢谢您的帮助!
--------------编辑-------------------
更具体地说,我将此用于记录目的以添加前缀消息:它只会在代码中出现一次!
所以它看起来更像是这样的:
void LogDebug(string msg)
{
Log("[DEBUG]", msg)
}
void Log(string pre, string msg)
{
// generates an output in form of
// pre + msg
}
Run Code Online (Sandbox Code Playgroud)
:)