Indexers and Properties


Indexer:

  1. The new concept in C# it is an object that acts as an array
  2. It is an object that is to be  indexed as an array
  3. Indexer modifier can be private, protected, public or internal
  4. The return type can be any valid C# data types
  5. Indexers in C# must have atleast one parameter
  6. Indexers are also known as smart arrays
  7. Ref and Out parameters must not be specified
  8. C# can overloaded just like member functions
  9. Indexer cannot be static


Syntax:

<modifier> <return type> this [argument list]
 {
  get
  {
   // Get codes goes here
  }
  set
  {
   // Set codes goes here
  }
 }

Example:

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

namespace IndexerDemo
{
    class ParentClass
    {
        private string[] range = new string[5];
        public string this[int index]
        {
            get
            {
                return range[index];
            }
            set
            {
                range[index] = value;
            }
        }
    }
    class ChildClass
    {
        static void Main(string[] args)
        {
            ParentClass pc = new ParentClass();
            pc[0] = "Mandala";
            pc[1] = "Vivek";
            pc[2] = "Ananda";
            pc[3] = "Swamy";
            pc[4] = "MVS";
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(pc[i]);
            }
        }
    }
}

Indexer and Inheritance

Just like other class members the indexers also can be inherited.

Example:

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

namespace IndexerDemo
{
    class IndexerClass
    {
        private string[] range = new string[5];
        public string this[int index]
        {
            get
            {
                return range[index];
            }
            set
            {
                range[index] = value;
            }
        }
    }
    class IndexerDemo1 : IndexerClass
    {
 
    }
    class IndexerTest
    {
        static void Main(string[] args)
        {
            IndexerDemo1 id = new IndexerDemo1();
            id[0] = "Mandala";
            id[1] = "Vivek";
            id[2] = "Ananda";
            id[3] = "Swamy";
            id[4] = "MVS";
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(id[i]);
            }
        }
    }
}

Indexers can be overridden in the derived class but you must use the virtual and override keywords in order to override the indexer in the derived clas

Example:

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

namespace IndexerDemo
{
    class BaseClass
    {
        private string[] b = new string[5];
        public virtual string this[int index]
        {
            get
            {
                return b[index];
            }
            set
            {
                b[index] = value;
            }
        }
    }
    class DerivedClass:BaseClass
    {
        private string[] d = new string[10];
        public override string this[int index]
        {
            get
            {
                return d[index];
            }
            set
            {
                d[index] = value;
            }
        }
    }
    class IndexerTest
    {
        static void Main(string[] args)
        {
            BaseClass dc = new DerivedClass();
            dc[0] = "12";
            dc[1] = "23";
            dc[2] = "34";
            dc[3] = "45";
            dc[4] = "56";
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(dc[i]);
            }
        }
    }  
}

Abstract Indexers

The Indexers in the class must be declared by the keyword Abstract.The abstract indexer does not contain any code.If the indexer contains only set or get then the derived class can implement only the set or get.

Example:

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

namespace IndexerDemo
{
    abstract class AbstractIndexer
    {
        private string[] a = new string[5];
        public abstract string this[int index]
        {
            get;
            set;
        }
    }
    class DerivedClass : AbstractIndexer
    {
        private string[] b = new string[5];
        public override string this[int index]
        {
            get
            {
                return b[index];
            }
            set
            {
                b[index] = value;
            }
        }
    }
    class IndexerTest
    {
        static void Main(string[] args)
        {
            DerivedClass dc = new DerivedClass();
            dc[0] = "132";
            dc[1] = "34";
            dc[2] = "43";
            dc[3] = "46";
            dc[4] = "87";
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(dc[i]);
            }
        }
    }
}

Properties in C#

  1. Properties are also known as the smart fields in C#
  2. They are the extensions of the C# data fields
  3. In a class we declare the datafields as private and will provide the public SET/GET methods to access the data fields
  4. The access modifier can be private,public ,protected or internal
  5. The return type can be any valid C# data type

Syntax of Properties

<acces_modifier> <return_type> <property_name>
{
       get
       {
        }
       set
       {
       }
}

Example:

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

namespace PropertiesDemo
{
    class PropertyDemo
    {
        private int x;
        public PropertyDemo(int i)
        {
            x = i;
        }
        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
    }
    class PropertyTest
    {
        static void Main(string[] args)
        {
            PropertyDemo pd = new PropertyDemo(40);
            Console.WriteLine("pd.x=" + pd.X);
        }
    }
}

C# supports static properties.All rules applicable to static members are also applicable to static properties also

Properties and Inheritence

The properties of the base class can be inherited to the derived class

Example:

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

namespace PropertiesDemo
{
    class PropertyBase
    {
      public virtual string X
      {
          get
          {
              return "Base Class";
          }         
      }
    }
    class PropertyDerived : PropertyBase
    {
        public override string X
        {
            get
            {
                return "Derived Class";
            }
        }
    }
    class PropertyTest
    {
        static void Main(string[] args)
        {
            PropertyBase pb = new PropertyDerived();
            Console.WriteLine("pd.x=" + pb.X);           
        }
    }
}

Abstract Properties:

A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors

Example:

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

namespace PropertiesDemo
{
    abstract class PropertyBase
    {
        public abstract string X
        {
            get;          
        }
    }
    class PropertyDerived : PropertyBase
    {
        public override string X
        {
            get
            {
                return "Derived Class";
            }           
        }
    }
    class PropertyTest
    {
        static void Main(string[] args)
        {
            PropertyBase pb = new PropertyDerived();
            Console.WriteLine("pd.x=" + pb.X);           
        }
    }
}


Similar Articles