.net - Is there any benefit on declaring a local constant of string over a local variable C# -


is there noticeable benefit regarding performance on using constant on string. suppose following case

public void methodusingconstant() {   const string searchedtext = "foo";    //some operations using searchedtext string }  public void methodnousingconstant() {   string searchedtext = "foo";    //some operations using searchedtext string } 

there option take class-struct constant field.

or, should avoid overthink these micro optimizations?

if think it, shouldn't different. because when computer sees variable, retrieves memory adress , value. have tried code , difference tiny (less 3ms: btw code ran in ~107 ms) down millions of other variables:

        //just changed non const         const string s = "hello";          string full = "";          int k = 0;          for(int = 0; < 10000; i++)         {             full += s;         } 

in general, const vs non const comes down this: value stay same: set const, else leave it. here great link .net optimization.

to sum up, should these tiny details moment code can't optimized in other way, , when case, code fine.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -