using System;
using System.Collections.Generic;
using System.Text;namespace 信号测控
{
    public class Channels     {
        public Channel this[int i]
        {            get
            {
                               
            }
           
        }        public int Count;
            }
}这个是我建索引器的类,get{},里面应该返回一个什么值呢,我是新手,请大家帮忙了

解决方案 »

  1.   

    你写了这个东西我也不知道你是什么意思,我给个例子给你看看:
    1.清单 11-1. 索引指示器的例子:IntIndexer.cs using System;
    /// 
    /// A simple indexer example.
    /// 
    class IntIndexer
    {
    private string[] myData;public IntIndexer(int size)
    {
    myData = new string[size];
    for (int i=0; i < size; i++)
    {
    myData[i] = "empty";
    }
    }
    public string this[int pos]
    {
    get
    {
    return myData[pos];
    }
    set
    {
    myData[pos] = value;
    }
    }static void Main(string[] args)
    {
    int size = 10;
    IntIndexer myInd = new IntIndexer(size);
    myInd[9] = "Some Value";
    myInd[3] = "Another Value";
    myInd[5] = "Any Value";
    Console.WriteLine("\nIndexer Output\n");
    for (int i=0; i < size; i++)
    {
    Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
    }
    }

    说明 
    1.清单 11-1演示了如何实现一个索引指示器, IntIndexer类有个名为myData的字符串数组,该数组是私有成员,因而其外部成员是看不见的。该数组是在构造函数中进行初始化的,该构造函数带有一个整型size参数,用来初始化myData数组,初始化时 把单词"empty"作为每个数组元素的值。
    2.IntIndexer类的下一成员是索引指示器(Indexer),由关键字this和方括号[int pos]标识出来。该成员带有一个位置参数pos。正如你已经猜测到,Indexer的实现同属性一样。Indexer有get 和set访问操作,就同属性中的用法一样。索引指示器(indexer)返回一个字符串,在定义索引指示器时,string这个类型名标志着其返回类型为字符串类型。
    3.Main()方法完成如下事情:初始化一个新的IntIndexer对象,添加一些值,并且打印出结果。其输出结果如下:
    Indexer Output
    myInd[0]: empty
    myInd[1]: empty
    myInd[2]: empty
    myInd[3]: Another Value
    myInd[4]: empty
    myInd[5]: Any Value
    myInd[6]: empty
    myInd[7]: empty
    myInd[8]: empty
    myInd[9]: Some Value 
    4.在不少程序语言中,通常都是使用整数作为下标来访问作为数组元素的,但C#的索引指示器不仅能够做到这一点,而且还能够更进一步。 定义索引指示器时,可以带有多个参数,每个参数的类型可以不同。添加的参数由逗号隔开,同方法中的的参数表一样。索引指示器的合法的参数类型包括:整型,枚举类型和字符串。另外,索引指示器也可以被重载。在清单 11-2中,我们修改了前面的程序,以便用来重载索引指示器 ,从而可以接受不同类型的参数。更详细的说明请查看如下网页:http://www.yxsoft.net.cn/Article/csharp/200705/7.html
      

  2.   

    索引器就是在功能上等效两个函数,所以按照对待函数的思想对待他们就可以了。
    按你的代码来看,只要get访问器return 一个Channel类型的对象即为合法的。具体返回什么东西
    要看你的需求了
      

  3.   

    恩,我的意思是这样的,一个channel类,然后channels是集合类,我引用的时候想出现这样的效果channels[i].[参数] , 每一个都返回一个channel类的实例,
    是在get{ return new channel();} 这样写么?我是菜鸟,请大家指教,多谢多谢
      

  4.   

    你的类里应该有一个数组(简单点是数组,复杂了可以是集合),return的是数组中的某项using System;
    using System.Collections.Generic;
    using System.Text;namespace 信号测控
    {
        public class Channels     {
            public Channel this[int i]
            {            get
                {
                      return a[i];              
                }
               
            }
            private int a = new int[5];
            public int Count;
                }
    }