Shantanu Gupta
Shantanu Gupta

Reputation: 21188

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(); }
}

Upvotes: 4

Views: 4799

Answers (2)

Aaronaught
Aaronaught

Reputation: 122624

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.

Upvotes: 14

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

C# 4 allows this, but only to access external COM properties..

Upvotes: 0

Related Questions