在方法中使用const

Jam*_*hes 5 .net c#

在以下方法中,

public void InspectList(IList<int> values)
{
    if(values != null)
    {
        const string format = "Element At {0}";
        foreach(int i in values)
        {
            Log(string.Format(format, i));
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

使用const是否比仅将字符串声明为字符串提供了任何好处?Woudl它还没有被实习?

Ode*_*ded 4

确实,在这两种情况下,它都会被拘留。

将其标记为 aconst会使您的意思更清晰 - do not touch this string variable, do not assign a different value to it