最近工作太忙,没有时间发帖,求教一个问题!!! (5/82)

< 上一篇下一篇 >
本帖地址: 复制地址

修改 回帖 引用 楼主: 壊/:D壞

用户形象图片

大吃兄和烟灰兄,如果你们看到小弟这个帖子,看有没有好的建议给我留个,,我在学习 索引器的时候遇到很大困难,不知道这是个怎么回事,俩位老大有什么好的例子或者文章给介绍个吧!!

回到帖子顶部

回帖 引用 1楼[楼主] 壊/:D壞

用户形象图片

虚心求教啊!!各位高手给点帮助吧!
回到帖子顶部

回帖 引用 2楼苦行僧

用户形象图片

索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
class SampleCollection<T>
{
    private T[] arr = new T[100];
    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer
class Program
{
    static void Main(string[] args)
    {
        SampleCollection<string> stringCollection = new SampleCollection<string>();
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
回到帖子顶部

回帖 引用 3楼苦行僧

用户形象图片

上面那个用了泛型,这个看起来要简单点哈:
using System;

namespace CsharpStepByStep
{
    /**//// <summary>
    /// Program 的摘要说明。
    /// </summary>
    public class Person
    {
        private string name;
        public Person(string Name)
        {
            name = Name;
        }

        public string Name
        {
            get {return name;}
        }
    }

    public class Authors
    {
        private Person[] persons = new Person[5];
        public Person this[int index]
        {
            get {return persons[index];}
            set {persons[index]=value;}
        }
    
    }

    public class SingleIndexer
    {
        public static void Main()
        {
            Authors authors = new Authors();
            authors[0] = new Person("aa");
            authors[1] = new Person("bb");
            authors[2] = new Person("cc");
            authors[3] = new Person("dd");
            authors[4] = new Person("ee");
            for(int i=0;i<5;i++)
            {
                Console.WriteLine(authors[i].Name);
            }
            Console.ReadLine();
        }
    }

}
回到帖子顶部

回帖 引用 4楼[楼主] 壊/:D壞

用户形象图片

我仔细看下吧!谢谢拉 !
回到帖子顶部

回帖 引用 5楼饼饼

用户形象图片

呵呵!!
恩,我明白了。
回到帖子顶部
个人信息
  • 荣誉+3
  • 荣誉+2
  • 荣誉+1
  • 荣誉-1
  • 荣誉-2
  • 荣誉-3
发表留言
  • 文章不错!
  • 精华好文!
  • 支持原创文章!
  • 帖子图文并茂,好!
  • 真知灼见,说得好!
  • 恶意广告
  • 违规内容
  • 严重灌水
  • 重复发帖
  • 标题党
你确定要删除此楼层吗
扣20点经验值

快速回复进入高级回复

插入图片 选择表情

验证码 看不清?换一张(不区分大小写)

[完成后按Ctrl+Enter发表]
[回复须知]