学习c#(第8天):c#中的索引器(一种实用方法)

表的内容 目录介绍c#中的索引器(定义)路线图索引器(解释) 实验一,实验二,实验三 数据类型在索引器 实验室1 接口中的索引器抽象类中的索引器索引器重载 点要记住 静态索引器? 点要记住 . net框架和索引器中的继承/多态 点要记住 属性vs索引器结论 介绍 在本系列的最后一篇文章中,我们学习了c#中的属性。“深入探究OOP”系列的这篇文章将解释关于c#中的索引器、它的使用和实际实现。我们将遵循同样的学习方法,即少理论多实践。我将试着深入地解释这个概念。 c#中的索引器(定义) 让我们从https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx获取定义, 索引器允许类或结构的实例像数组一样被索引。索引器重复属性,除了它们的访问器接受参数。” 路线图 让我们回顾一下我们的路线图, 潜水在OOP(第一天):多态和继承(早期绑定/编译时多态)潜水在OOP(第二天):多态和继承(继承)潜水OOP(第三天):多态和继承(动态绑定/运行时多态)潜水在OOP(第四天):多态和继承(关于Abstarct类在c#中)潜水在OOP(第五天):都是用c#访问修饰符(公共/私人/保护/内部/密封/常量/只读的字段)学习c#(6天):理解枚举在c#中(一个实际的方法)学习c#(7天):用c#属性(一个实际的方法)学习c#(8天):用c#索引器(一个实际的方法)学习c#(9天):了解c#事件(Insight)学习c#(第十天):代表在c#中(一个实际的方法)学习c#(11天):c#事件(一个实际的方法) 索引器(解释) 正如定义所说,索引器允许我们利用将类对象作为数组访问的能力。 为了更好地理解,创建一个名为Indexers的控制台应用程序,并向其添加一个名为Indexer的类。我们将使用这个类和项目来学习索引器。使类公开,现在不添加任何代码,在Program.cs中添加以下代码, 实验室1 隐藏,复制Code

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            indexer[1] = 50;
        }
    }
}

编译代码。我们得到, 不能用[]对'Indexers.Indexer'类型的表达式应用索引 我刚刚创建了Indexer类的一个对象,并试图将该对象用作数组。因为它实际上不是一个数组,所以导致了编译时错误。 实验室2 Indexer.cs 隐藏,复制Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
    public class Indexer
    {
        public int this[int indexValue]
        {
            set
            {
                Console.WriteLine("I am in set : Value is " + value + " and indexValue is " + indexValue);
                Console.ReadLine();
            }
        }
    }
}

Program.cs 隐藏,复制Code

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            indexer[1] = 50;
        }
    }
}

输出 这里我们使用indexer来索引类indexer的对象。现在我的对象可以作为数组来访问不同的对象值。 索引器的实现来自一个名为“this”的属性。它接受一个整型参数indexValue。索引器不同于属性。在属性中,当我们想要初始化或赋值时,会自动调用“set”访问器(如果定义的话)。而“set”访问器中的关键字“value”用于保存或跟踪分配给我们的属性的值。在上面的例子中,索引器[1]= 50; 调用“this”属性的“set”访问器,即一个索引器,因此50成为值,1成为该值的索引。 实验室3 Indexer.cs 隐藏,复制Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
    public class Indexer
    {
        public int this[int indexValue]
        {
            set
            {
                Console.WriteLine("I am in set : Value is " + value + " and indexValue is " + indexValue);
            }
             get
            {
                Console.WriteLine("I am in get and indexValue is " + indexValue);
                return 30;
            }
        }
    }
}

Program.cs 隐藏,复制Code

using System;

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            Console.WriteLine(indexer[1]);
            Console.ReadKey();
        }
    }
}

输出 在上面的代码片段中,我也使用get来访问indexer的值。属性和索引器在同一组规则上工作。我们在如何使用它们上有一点不同。当我们执行indexer[1]时,意味着调用“get”访问器,当我们赋值给indexer[1]时,意味着调用“set”访问器。在实现indexer代码时,我们必须注意,当我们访问indexer时,它是以变量和数组参数的形式访问的。 数据类型在索引器 实验室1 Indexer.cs 隐藏,复制Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
    public class Indexer
    {
        public int Index;
        public int this[string indexValue]
        {
            set
            {
                Console.WriteLine("I am in set : Value is " + value + " and indexValue is " + indexValue);
                Index = value;
            }
             get
            {
                Console.WriteLine("I am in get and indexValue is " + indexValue);
                return Index;
            }
        }
    }
}

Program.cs 隐藏,复制Code

using System;

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            indexer["name"]=20;
            Console.WriteLine(indexer["name"]);
            Console.ReadKey();
        }
    }
}

输出 “this”属性,即索引器有返回值。在我们的示例中,返回值是整数。方括号和“this”也可以容纳其他数据类型,而不仅仅是整数。在上面提到的例子中,我试图用字符串参数类型来解释“this”:public int this[string indexValue], 字符串参数“indexValue”有一个值“name”,就像我们在Program.cs的Main方法中传递的一样。因此,一个类中可以有多个索引器来决定数组的参数值的数据类型。与属性一样,索引器遵循同样的继承和多态性规则。 索引器的接口 与属性和方法一样,索引器也可以在接口中声明。 在实际实现中,只需创建一个名为IIndexers的接口,其中包含以下代码, 隐藏,复制Code

namespace Indexers
{
    interface IIndexers
    {
        string this[int indexerValue] { get; set; }
    }
}

在这里,一个索引器是用一个空的获取和设置声明访问器,返回字符串值。 现在我们需要一个类来实现这个接口。您可以定义一个类的选择和实现,通过IIndexers接口, Indexer.cs 隐藏,复制Code

using System;

namespace Indexers
{
    public class IndexerClass:IIndexers
    {
        readonly string[] _nameList = { "AKhil","Bob","Shawn","Sandra" };

        public string this[int indexerValue]
        {
            get
            {
                return _nameList[indexerValue];
            }
            set
            {
                _nameList[indexerValue] = value;
            }
        }
    }
}

类都有一个默认的字符串数组名称。现在我们可以在这个类来实现接口定义索引器获取写我们自定义的逻辑名称indexerValue的基础上。我们叫它在我们的主要方法, Program.cs 隐藏,复制Code

using System;

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
           IIndexers iIndexer=new IndexerClass();
            Console.WriteLine(iIndexer[0]);
            Console.WriteLine(iIndexer[1]);
            Console.WriteLine(iIndexer[2]);
            Console.WriteLine(iIndexer[3]);
            Console.ReadLine();

        }
    }
}

运行应用程序。输出, 在主方法中,我们把IndexerClass接口来创建一个对象的引用,我们访问对象数组通过索引值数组。它给的名字。 现在如果我想访问“套”访问,我可以很容易地做到这一点。检查这个,只是添加两行,你在索引器设置值, 隐藏,复制Code

iIndexer[2] = "Akhil Mittal";
Console.WriteLine(iIndexer[2]);

我2号元素的值设置为一个新名字,让我们看看输出, 索引器在抽象类 就像我们使用索引器接口,我们也可以在抽象类中使用索引器。我将使用相同的逻辑我们使用的源代码接口,这样你可以在抽象类与它是如何工作的。仅定义一个新类,抽象和应该包含一个抽象的索引器与空的获取和设置, AbstractBaseClass 隐藏,复制Code

namespace Indexers
{
    public abstract class AbstractBaseClass
    {
        public abstract string this[int indexerValue] { get; set; }
    }
}

定义派生类,继承抽象类, IndexerClass 我们这里使用覆盖在索引器覆盖文摘索引器中声明抽象类。 隐藏,复制Code

using System;

namespace Indexers
{
    public class IndexerClass:AbstractBaseClass
    {
        readonly string[] _nameList = { "AKhil","Bob","Shawn","Sandra" };

        public override string this[int indexerValue]
        {
            get
            {
                return _nameList[indexerValue];
            }
            set
            {
                _nameList[indexerValue] = value;
            }
        }
    }
}

Program.cs 我们将使用抽象类来创建一个对象的引用的索引器类。 隐藏,复制Code

using System;

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
           AbstractBaseClass absIndexer=new IndexerClass();
            Console.WriteLine(absIndexer[0]);
            Console.WriteLine(absIndexer[1]);
            Console.WriteLine(absIndexer[2]);
            Console.WriteLine(absIndexer[3]);
            absIndexer[2] = "Akhil Mittal";
            Console.WriteLine(absIndexer[2]);

            Console.ReadLine();

        }
    }
}

输出: 上面的代码是自解释的。你可以自己探索更多的场景更详细的理解。 索引器过载 Indexer.cs 隐藏,收缩,复制Code

using System;

namespace Indexers
{
    public class Indexer
    {
        public int this[int indexerValue]
        {
            set
            {
                Console.WriteLine("Integer value " + indexerValue + " " + value);
            }
        }

        public int this[string indexerValue]
        {
            set
            {
                Console.WriteLine("String value " + indexerValue + " " + value);
            }
        }

        public int this[string indexerValue, int indexerintValue]
        {
            set
            {
                Console.WriteLine("String and integer value " + indexerValue + " " + indexerintValue + " " + value);
            }
        }
    }
}

Program.cs 隐藏,复制Code

using System;

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            indexer[1] = 30;
            indexer["name"]=20;
            indexer["address",2] = 40;
            Console.ReadLine();
        }
    }
}

输出 在上面的示例中,我们看到一个索引器的签名在实际计算的实际参数和数据类型irresepective参数的名称/索引器的参数或返回值。这让我们像我们在方法重载超载索引器。你可以阅读更多关于方法加载在http://www.codeproject.com/Articles/771455/Diving-in-OOP-Day-Polymorphism-and-Inheritance-Ear。现在我们有重载分度器,整数,整数字符串和字符串组合作为实际参数。这样的方法不能被重载的返回类型的基础上,所以分度器遵循相同的方法做过载的方法。 点要记住 索引器一样,我们不能过载特性。属性更像是知道的名字和索引器另一方面更像是知道签名。 静态索引器? 在这个例子中,我们在最后一节中,讨论静态关键字添加到索引器的签名, 隐藏,复制Code

public static int this[int indexerValue]
  {
      set
      {
          Console.WriteLine("Integer value " + indexerValue + " " + value);
      }
  }

编译程序;我们得到一个编译时错误, 错误的修饰符“静态”是无效的条目 错误显然表明一个索引器不能被标记为静态的。一个索引器只能一个类实例成员而不是静态的,另一方面羽根属性也可以是静态的。 点要记住 属性可以是静态的,但索引器不能。 在索引器继承和多态性 Indexer.cs 隐藏,收缩,复制Code

using System;

namespace Indexers
{
    public class IndexerBaseClass
    {
        public virtual int this[int indexerValue]
        {
            get
            {
                Console.WriteLine("Get of IndexerBaseClass; indexer value: " + indexerValue);
                return 100;
            }
            set
            {
                Console.WriteLine("Set of IndexerBaseClass; indexer value: " + indexerValue + " set value " + value);
            }

        }
    }
    public class IndexerDerivedClass:IndexerBaseClass
    {
        public override int this[int indexerValue]
        {
            get
            {
                int dValue = base[indexerValue];
                Console.WriteLine("Get of IndexerDerivedClass; indexer value: " + indexerValue + " dValue from base class indexer: " + dValue);
                return 500;
            }
            set
            {
                Console.WriteLine("Set of IndexerDerivedClass; indexer value: " + indexerValue + " set value " + value);
                base[indexerValue] = value;
            }

        }
    }
}

Program.cs 隐藏,复制Code

using System;

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            IndexerDerivedClass indexDerived=new IndexerDerivedClass();
            indexDerived[2] = 300;
            Console.WriteLine(indexDerived[2]);
            Console.ReadLine();

        }
    }
}

输出 上面的示例代码解释了在索引器运行时多态和继承。我创建了一个名为IndexerBaseClass拥有一个索引器的基类有自己的获取和设置就像我们之前讨论的例子。之后有一个名叫IndexerDerivedClass创建派生类,这来源于IndexerBaseClass覆盖”这种“从基类索引器,注意,基类索引器是虚拟的,所以我们可以在派生类重写它标志着它在派生类“覆盖”。这个例子使调用索引器的基类。有时候我们需要在派生类的派生类重写代码,我们可能需要基类索引器应该被称为第一。这只是一个情况。运行时多态的相同规则适用于这里,我们声明索引器和虚拟基类和派生类重写。在“设置”访问派生类,我们可以调用基类索引器作为基础[indexerValue]。也使用这个值来初始化派生类索引器。值是存储在“价值”关键字。所以,indexDerived [2] Program.cs被取代了Main()方法的基础[2]在“设置”访问器。而在“获得”访问器亦然,我们需要把基础(indexerValue)等号的右手边。基类中的“获得”访问器返回一个值,即100,我们得到在dValue变量。 net框架和索引器 索引器在。net框架中起到至关重要的作用。索引器广泛应用于。net框架内置类,图书馆收藏和enumerab等勒。索引器使用可搜索的集合就像字典,散列表,列表,Arraylist等等。 点要记住 字典在c#中主要使用索引器盯着参数作为一个索引器参数。 内部类数组列表和列表使用索引器提供的功能为获取和使用元素数组。 属性和索引器 我已经解释了很多关于属性和索引器,总而言之,让我点为更好地理解一个MSDN的链接, PropertyIndexerAllows方法被当作公共数据成员。允许内部对象的集合的元素被使用数组访问符号对象本身。通过一个简单的名字。通过索引访问。可以是静态或实例成员。必须是类的一个实例member.A  get 属性的访问器没有parameters.A  get 访问器的索引器都具有相同的形式参数列表indexer.A  set 属性的访问器包含implicit  value  parameter.A  set 访问器的索引器都具有相同的形式参数列表索引器,并从而值参数。支持语法with 缩短;自动实现属性(c#编程指南)。不支持缩短语法。 表从MSDN: https://msdn.microsoft.com/en-us/library/4bsztef7.aspx 结论 在本文中我们完成了几乎所有的场景相关的索引器。我们实验室做了很多实践明确概念。我希望我的读者现在用心了解这些基本概念和永远不会忘记他们。这些也可以帮助你在c#采访。 保持编码和享受阅读 也不要忘记评价/评论/喜欢我的文章,如果以任何方式帮助你,这有助于我得到激励,鼓励我写的越来越多。 我的其他系列文章: MVC: http://www.codeproject.com/Articles/620195/Learning-MVC-Part-Introduction-to-MVC-Architectu RESTful webapi: http://www.codeproject.com/Articles/990492/RESTful-Day-sharp-Enterprise-Level-Application 编码快乐! 本文转载于:http://www.diyabc.com/frontweb/news1972.html

原文地址:https://www.cnblogs.com/Dincat/p/13456745.html