开发者

How can we create a parameterized properties in C#

开发者 https://www.devze.com 2022-12-30 00:16 出处:网络
How can I create a parameterized properties in C#. public readonly string ConnectionString(string ConnectionName)

How can I create a parameterized properties in C#.

public readonly string ConnectionString(string ConnectionName)
{
    get { return System.Configuration.ConfigurationManager.ConnectionStrings[开发者_StackOverflowConnectionName].ToString(); }
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消