Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


const and readonly in .NET

baranbaran

I never thought that I would use readonly identifier in C#. But here is a valid reason to use it.

public const string SQL_CONNECTION_STRING = ConfigurationManager.ConnectionStrings["MyWebConnectionString"].ConnectionString;

When I store SQL_CONNECTION_STRING as a constant (due to design issue) and want to pass the connection string in app.config, it fails if i keep it as const. The solution is to change it to readonly.

public readonly string SQL_CONNECTION_STRING = ConfigurationManager.ConnectionStrings["MyWebConnectionString"].ConnectionString;