MVC08

1. c# 索引器(indexer)

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private string[] nameList = new string[10];
        static void Main(string[] args)
        {

            var names = new IndexedNames();
            names[0] = "hi";
            Console.WriteLine(names[0]);
            Console.WriteLine(names[1]);
            Console.WriteLine(names[11]);
        }

        class IndexedNames
        {
            private string[] nameList = new string[10];
            public IndexedNames()
            {
                for (int i = 0; i < nameList.Length; i++)
                {
                    nameList[i] = "N/A";
                }
            }

            public string this[int index]
            {
                get
                {
                    string tmp;
                    if (index >= 0 && index < nameList.Length)
                    {
                        tmp = nameList[index];
                    }
                    else
                    {
                        tmp = "index is empty";
                    }
                    return tmp;
                }

                set
                {
                    if(index >= 0 && index < nameList.Length)
                    {
                        nameList[index] = value;
                    }
                }

            }
        }

    }
}

 

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private string[] nameList = new string[10];
        static void Main(string[] args)
        {

            var names = new IndexedNames();
            names[0] = "hi";
            Console.WriteLine(names[0]);
            Console.WriteLine(names[1]);
            Console.WriteLine(names[11]);

            var testStr = "hi";
            Console.WriteLine(names[testStr]);

        }

        class IndexedNames
        {
            private string[] nameList = new string[10];
            public IndexedNames()
            {
                for (int i = 0; i < nameList.Length; i++)
                {
                    nameList[i] = "N/A";
                }
            }

            public string this[int index]
            {
                get
                {
                    string tmp;
                    if (index >= 0 && index < nameList.Length)
                    {
                        tmp = nameList[index];
                    }
                    else
                    {
                        tmp = "index is empty";
                    }
                    return tmp;
                }

                set
                {
                    if(index >= 0 && index < nameList.Length)
                    {
                        nameList[index] = value;
                    }
                }

            }

            // 以字符串为索引,返回该字符串在数组中的整型索引,重载了索引器
            public int this[string name]
            {
                get
                {
                    int i = 0;
                    while(i < nameList.Length)
                    {
                        if(nameList[i] == name)
                        {
                            return i;
                        }
                    }
                    return -1;
                }
            }
        }

    }
}

 可为get、set 设置修饰符,一般set为private,get为public

3. 基于接口的索引器与代码强壮性

在接口内也可以新建索引器。

下方

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private string[] nameList = new string[10];
        static void Main(string[] args)
        {

            var names = new IndexedNames();
            names[0] = "hi";
            Console.WriteLine(names[0]);
            Console.WriteLine(names[1]);
            Console.WriteLine(names[11]);

            var testStr = "hi";
            Console.WriteLine(names[testStr]);

        }
    }
    class IndexedNames
    {
        private string[] nameList = new string[10];
        public IndexedNames()
        {
            for (int i = 0; i < nameList.Length; i++)
            {
                nameList[i] = "N/A";
            }
        }

        public string this[int index]
        {
            get
            {
                string tmp;
                if (index >= 0 && index < nameList.Length)
                {
                    tmp = nameList[index];
                }
                else
                {
                    tmp = "index is empty";
                }
                return tmp;
            }

            set
            {
                if (index >= 0 && index < nameList.Length)
                {
                    nameList[index] = value;
                }
            }

        }

        // 以字符串为索引,返回该字符串在数组中的整型索引
        public int this[string name]
        {
            get
            {
                int i = 0;
                while (i < nameList.Length)
                {
                    if (nameList[i] == name)
                    {
                        return i;
                    }
                }
                return -1;
            }
        }
    }
    public interface ISomeInterface
    {
        int this[int index]
        {
            get;
            set;
        }
    }
    class IndexerClass : ISomeInterface
    {
        private int[] arr = new int[100];
        public int this[int index]
        {
            get
            {
                return arr[index];
            }
            set
            {
                arr[index] = value;
            }
        }
    }
}

 

 

------------恢复内容结束------------

你可能感兴趣的:(MVC08)