【问题标题】:C#: public method{get{} set{}} questionC#:公共方法{get{} set{}} 问题
【发布时间】:2009-09-24 13:09:30
【问题描述】:

我不完全确定我的所有术语是否正确,如果我错了,请原谅我。我想知道是否可以向该方法发送参数。以以下为例。

public item (int index)  
{  
    get { return list[index]; }  
    set { list[index] = value; }  
}

我知道它会出错。我要问的是是否有某种方法可以让它工作。有什么建议或者我应该想办法解决吗?

提前致谢。

【问题讨论】:

标签: c# .net indexer


【解决方案1】:

试试这个:

// This returns an instance of type "Foo",
// since I didn't know the type of "list".
// Obviously the return type would need to
// match the type of whatever "list" contains.
public Foo this[int index]
{
    get { return list[index]; }
    set { list[index] = value; }
}

这是 C# 的 indexer 语法,它有一些限制(它不如 VB.NET 的参数属性灵活),但它确实适用于您的具体示例。

【讨论】:

  • 哇,这很容易。像魅力一样工作。谢谢!
  • 另外,list 只是 List,这会改变什么吗?
  • 然后将Foo替换为Color
  • 它只会改变参数的返回类型——我已经编辑了我的答案来反映这一点。
【解决方案2】:

正如其他人所展示的,您可以将其变成一个索引器 - 顺便说一下,它可以有多个参数。

不能做的是在 C# 中命名一个索引器......虽然你可以在 VB 中。所以你不能有两个索引器,一个叫做Foo,另一个叫做Bar...你需要编写返回值的属性,这些值本身就是可索引的。老实说,这有点痛苦:(

【讨论】:

  • +1 给你。不过,我从来没有完全理解这一点——在 VB 中,调用参数属性的语法看起来就像一个方法调用(当然是这样),那么优势是什么?就这样它看起来和行为都像一个方法,但在程序集中会有“属性”元数据?
  • @Andrew 您可以将赋值语法与属性一起使用:Product.Description("en-US") = "Tea"
  • 这当然可以作为 Product.SetDescription("en-US", "Tea")。针对参数属性的参数与针对一般属性的参数完全相同 - 它们只是方法调用,您可以将它们称为方法(如在 Java 中)等。
  • 这是有道理的——我想唯一奇怪的是,至少 C# 的索引器 看起来 不同,这有助于提高可读性。 VB 语法很容易混淆,因为它与方法没有区别。
【解决方案3】:

这称为索引器属性

public int this [int index]  
{      
     get { return list[index]; }      
     set { list[index] = value; }  
}

【讨论】:

    【解决方案4】:

    我认为您可能正在寻找的是:

    public Something this[int index]
    {
        get
        {
             return list[index];
        }
        set
        {
             list[index] = value;
        }
    }
    

    【讨论】:

      【解决方案5】:

      作为记录,虽然其他答案是有效的,但您可能还需要考虑使用以下方法:

      public IList<Something> items { get; set; }
      

      然后可以按如下方式使用:

      Something item = myFoo.items[1];
      

      其他答案的使用方式略有不同:

      Something item = myFoo[1];
      

      你想要的取决于你到底想达到什么,如果不看剩下的代码就很难确定。

      【讨论】:

        【解决方案6】:

        除了现在已经多次提到的索引器之外,另一种可能性是使用索引器制作自定义类并将其实例作为属性返回。

        例子:

        public class IntList
        {
            public IntList(IEnumerable<int> source)
            {
                items = source.ToArray();
                Squares = new SquareList(this);
            }
        
            private int[] items;
        
            // The indexer everyone else mentioned
            public int this[int index]
            {
                get { return items[index]; }
                set { items[index] = value; }
            }
        
            // Other properties might be useful:
            public SquareList Squares { get; private set; }
        
            public class SquareList
            {
                public SquareList(IntList list)
                {
                    this.list = list;
                }
        
                private IntList list;
        
                public int this[int index]
                {
                    get { return list.items[index] * list.items[index]; }
                }
            }
        }
        

        【讨论】:

          【解决方案7】:

          你可以使用索引器来解决这个问题

          public object this[string name]
                  {
                      get
                      {
                          int idx = FindParam(name);
                          if (idx != -1)
                              return _params[idx].Value;
          
                          throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
                      }
                      set 
                      { 
                          int idx = FindParam(name);
                          if (idx != -1)
                              _params[idx].Value = value;
                          else
                              throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-31
            • 2012-12-09
            • 2011-02-28
            • 1970-01-01
            • 2010-11-23
            • 2013-12-26
            • 1970-01-01
            • 2013-04-18
            相关资源
            最近更新 更多