const string inside a function
A snippet from Mricrosoft XNA Education Catalog:
/// <summary>
/// Draws the control, using SpriteBatch and SpriteFont.
/// </summary>
protected override void Draw()
{
const string message = "Hello, World!\n" +
"\n" +
"I'm an XNA Framework Gr开发者_开发百科aphicsDevice,\n" +
"running inside a WinForms application.\n" +
"\n" +
"This text is drawn using SpriteBatch,\n" +
"with a SpriteFont that was loaded\n" +
"through the ContentManager.\n" +
"\n" +
"The pane to my right contains a\n" +
"spinning 3D triangle.";
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
spriteBatch.End();
}
Draw is called 60 times per second. Is there any performance overhead with assigning message inside the draw? Is it the same as if I move it to static helper class? As far I recall, cost expression is evaluated by C# compiler. What const modifier change here?
A const is evaluated once only. You gain nothing by moving it away into a static variable.
Nope its all optimized for you.
精彩评论