Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

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;