当前位置:  编程技术>.net/c#/asp.net

C#索引器简单实例代码

    来源: 互联网  发布时间:2014-10-17

    本文导语:  代码如下:public class Fruit {         string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on";         string orange = "a round fruit that has a thick orange skin and is divided into parts...

代码如下:

public class Fruit

{

        string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on";

        string orange = "a round fruit that has a thick orange skin and is divided into parts inside";

        string banana = "a long curved tropical fruit with a yellow skin";

        string apple = "a hard round fruit that has red, light green, or yellow skin and is white inside ";

        public string this[string fruitName]

        {

            get

            {

                switch (fruitName)

                {

                    case "peach":

                        return peach;

                    case "orange":

                        return orange;

                    case "banana":

                        return banana;

                    case "apple":

                        return apple;

                    default:

                        throw new Exception("wrong fruit name");

                }

            }

            set

            {

                switch (fruitName)

                {

                    case "peach":

                        peach = value;

                        break;

                    case "orange":

                        orange = value;

                        break;

                    case "banana":

                        banana = value;

                        break;

                    case "apple":

                        apple = value;

                        break;

                    default:

                        throw new Exception("wrong fruit name");

                }

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Fruit f = new Fruit();

            //关联数组的方式访问get方法

            Console.WriteLine(f["peach"]);

            //关联数组的方式访问set方法

            f["peach"] = "I like to eat peach.";

            Console.WriteLine(f["peach"]);

            Console.ReadLine();

        }

    }


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 在RowCommand事件中获取索引值示例代码
  • SQLSERVER 创建索引实现代码
  • android 左右滑动+索引图标实现方法与代码
  • C++ Strings(字符串) 成员 at():按给定索引值返回字符
  • mysql 添加索引 mysql 如何创建索引
  • Mysql索引类型:B-Tree索引介绍
  • MYSQL索引无效和索引有效的详细介绍
  • Mysql索引类型:Hash索引介绍及举例说明
  • mysql下普通索引和唯一索引的效率对比
  • MySQL索引基本知识
  • 在线等待:如何把vector清空,再用该清空vector是,索引是从0开始,而不是紧接着上次清空是的索引?
  • MySQL Hash索引和B-Tree索引的区别
  • SQL Server 索引结构及其使用(一)--深入浅出理解索引结构第1/4页
  • MySQ索引操作命令总结(创建、重建、查询和删除索引命令详解)
  • MYSQL索引建立需要注意以下几点细节
  • bitmap 索引和 B-tree 索引在使用中如何选择
  • Mysql索引会失效的几种情况分析
  • 数据收集和索引系统 Moloch
  • 文件索引
  • mysql 表索引的一些要点
  • bitmap索引压缩处理 FastBit
  • 从Oracle的约束到索引
  • sql server对索引的使用
  • 索引的原理及索引建立的注意事项


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3