如何在C#中进行参数化属性 [英] how to make parameterized properties in c#

查看:73
本文介绍了如何在C#中进行参数化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中制作参数化属性

how to make parameterized properties in c#

推荐答案

以下是在MSDN中找到的示例:

here is an example found in MSDN:

public WebSite this[int index]
{
   get
      {    
          if (index > sites.Count)    
            return (WebSite)null;    

          return (WebSite) sites.GetByIndex(index);
      }
   set
      {    
         if ( index < 10 )    
           sites[index] = value;
      }
}



如果我没记错的话,唯一可以参数化的属性就是索引器.



The only parametrized properties you can make are indexers if I am not mistaken.


您可以在C#中创建的参数化属性的唯一类型是索引器属性:

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


有关更多信息,请参考: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx [^ ]


否则,只需创建一个方法即可-似乎更接近您要查找的方法,如:
中所示 http://social.msdn.microsoft.com /Forums/zh-CN/csharplanguage/thread/1c813b3e-7049-465c-8439-61e37e8a07ba/ [


for more refer:http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx[^]


Otherwise, just create a method instead - that seems to be closer to what you are looking for, a shown in :
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/1c813b3e-7049-465c-8439-61e37e8a07ba/[^]


这篇关于如何在C#中进行参数化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆