SIGN UP MEMBER LOGIN:    
ARTICLE

Indexers and Properties

Posted by vivekananda swamy Articles | ASP.NET Programming July 13, 2009
Indexers and Properties the new features in C#, also known as Smart arrays and Smart fields.This article describes the detailed features of Indexers and Properties
Reader Level:

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);           
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor