A Program of Indexer in Console in ASP.NET using C#

Indexer: Indexer can be termed as location indicators and they are used to access class objects in the same way as array elements are accessed.

Difference between Property and Indexers:
1165-439-new.png
Figure 1. 1165-439-new.png

Indexers have the following feature:

Inheritance: Allows Inheritance, which means a derived class can inherit a base class indexer. Modifier Such as virtual and override are used at the property level, not at the accessor level.
Polymorphism: Allows Polymorphism, which means the derived class can override a base class indexer.
Uniqueness: Provides a signature that uniquely identifies an indexer.
Non Static: Indexers cannot be static. If you declare indexer as static, the compiler will generate an error.

Here is the code for Indexer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Indexers
{
    class sample
    {
        string[] name = new string[3];
        public string this[int index]
        {
            get
            {
                if (index < 0 || index >= name.Length)
                {
                    return null;
                }
                else
                {
                    return (name[index]);
                }
            }
            set
            {
                name[index] = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            sample s = new sample();
            s[0]=“w3asp”;
            s[1]=“w3mirchi”;
            s[2]=“w3shout”;
            for (int i = 0; i <= 2; i++)
                Console.WriteLine(s[i]);
            Console.ReadKey();
        }
    }
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*