I am using sqlite in my app. I am declaring my sqliteConnection as constant. But the problem is that the user of this class can change the connectionString property of this sqliteConnection object. How I can prevent that? I want the connection string of the connection to be set just once.
1 Answer
You can create a read-only property...
private string connectionString = "my_string";
public string ConnectionString
{
get { return connectionString; }
}
Or a readonly field. The value can be set only in the constructor or as a constant:
public readonly ConnectionString = "my_string";
Or a const field. The value can be set as a constant (value is static, i.e. there will only be one value for all instances):
public const ConnectionString = "my_string";
1 Comment
p.s.w.g
Pretty sure you can't do
readonly and const on one field.
constkeyword, it will be readonly by default.