C#复习之索引器

知识点一:索引器基本概念

基本概念:

让对象可以像数组一样通过索引访问其中元素,使程序看起来更直观,更容易编写

知识点二:索引器语法

//value代表传入的值

知识点三:索引器的使用

知识点四:索引器中可以写逻辑

知识点五:索引器可以重载

重载的概念是---函数名相同 参数类型、数量、顺序不同

可以把this看成函数名

总结:

索引器对于我们来说的主要作用

可以让我们以中括号的形式范围自定义类中的元素 规则自己定 访问时和数组一样

比较适用于 在类中有数组变量时使用 可以方便的访问和进行逻辑处理

固定写法

访问修饰符 返回值 this参数列表

get和set语句块

可以重载
注意:结构体里面也是支持索引器

练习题:自定义一个整形数组类,该类中有一个整形数组变量,为它封装增删查改的方法

cpp 复制代码
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 索引器
{
    internal class Program
    {
        static void Main(string[] args)
        {
           IntArray array = new IntArray();
            array.Add(100);
            array.Add(200);
            array.Add(300);
            array.Add(400);
            array.Add(500);
            Console.WriteLine(array[0]);
            array.RemoveAt(0);
            Console.WriteLine(array[1]);
            array.Remove(200);
            Console.WriteLine(array[1]);
            Console.WriteLine(array[6]);
            Console.WriteLine(array.Length);
        }
    }

    class IntArray
    {
        private int[] array;

        private int capacity;//房间容量
        private int length;//长度(当前放了几个房间)

        public IntArray()
        {
            capacity = 5;
            length = 0;
            array = new int[capacity];
        }
        //增
        public void Add(int value)
        {
            //如果要增加就涉及扩容
            //扩容就涉及"搬家"
            if (length<capacity)
            {
                array[length] = value;
                ++length;
            }
            //扩容
            else
            {
                capacity *= 2;
                //新房子
                int[] tempArray =new int[capacity];
                //老东西放进新房子里
                for (int i = 0; i < array.Length; i++)
                {
                    tempArray[i] = array[i];
                }
                //老的房子地址 指向新房子的地址
                array = tempArray;

                //往后面放
                array[length] = value;
                ++length;
            }
        }
        //删
        public void Remove(int value)
        {
            //找到传入值在哪个位置
            for (int i = 0; i < length; i++)
            {
                if (array[i] == value)
                {
                    RemoveAt(i);
                    return;
                }
            }
            Console.WriteLine("没有在数组中找到{0}", value);
        }
        public void RemoveAt(int index)
        {
            if (index>length-1)
            {
                Console.WriteLine("当前数组只有{0},你越界了", length);
                return;
            }
            for (int i = index; i < length-1; i++)
            {
                array[i] = array[i + 1];
            }
            --length;
        }
        //查改
        public int this[int index]
        {
            get {
                if (index>=length||index<0)
                {
                    Console.WriteLine("越界");
                    return 0;
                }
                return array[index]; }
            set {
                if (index>=length||index<0)
                {
                    Console.WriteLine("越界");
                }
                array[index] = value;}
        }
        public int Length
        {
            get { return length; }
        }
    }
}
相关推荐
rockey6277 小时前
AScript正式推出Template模板引擎
c#
沐言Agent9 小时前
做工业上位机,这个 C# 开源框架把 PLC 通讯、流程控制和 MES 对接都备好了
c#·工业上位机
投研帮17 小时前
C#文档管理系统开发实战:技术选型、全文检索与并发控制
c#·.net·文档管理系统
星恒随风1 天前
从0实现C++ AI大模型接入SDK(四):大模型API接入、公共数据结构设计与日志库封装
linux·服务器·数据结构·c++·人工智能
2395730491 天前
在 Avalonia 12 上用纯 .NET 重写 Monaco 编辑器:TKCode.Arc CodeEditor / DiffEditor / LSP 深度架构与实战
c#·.net·lsp·avalonia·monaco·codeeditor·tkcode.arc
AIgorithmGEEK2 天前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
饮水桶_T2 天前
HashMap笔记
开发语言·数据结构·笔记
风落青芜2 天前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
h_a_o777oah2 天前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
新*2 天前
在 .NET 里嵌入 IronPython:内存只涨不降的真相与正确的封装姿势
c#·.net·内存优化·ironpython·dlr