Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we create a parameterized properties in C#

How can I create a parameterized properties in C#.

public readonly string ConnectionString(string ConnectionName)
{
    get { return System.Configuration.ConfigurationManager.ConnectionStrings[ConnectionName].ToString(); }
}
like image 395
Shantanu Gupta Avatar asked Dec 09 '22 16:12

Shantanu Gupta


1 Answers

The only type of parameterized property you can create in C# is an indexer property:

public class MyConnectionStrings
{
    private string GetConnectionString(string connectionName) { ... }

    public string this[string connectionName]
    {
        get { return GetConnectionString(connectionName); }
    }
}

Otherwise, just create a method instead - that seems to be closer to what you are looking for.

like image 131
Aaronaught Avatar answered Dec 25 '22 07:12

Aaronaught