Saturday 4 January 2014

What is Indexers in C# .Net?

What is Indexers in C# .Net?

In c# introduce new concept is Indexer. It allows your class to be used just like an array. On the inside of a class, you manage a collection of values any way you want. This is very useful for some situation. Let as discuss something about Indexer.
  • Indexer Concept is object act as an array.
  • Indexer an object to be indexed in the same way as an array.
  • Indexer modifier can be private, public, protected or internal.
  • The return type can be any valid C# types.
  • Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.
General Syntax of Indexers..................

[access-modifier] return-type this [Type of Indexer]
{    get{   }
     set {   }
}

Practical implementation with Indexers in C# .Net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IndexerConcept
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//create class which name is IntCollection
class IntCollection
{
private int[] arr = new int[100];
private int index = 0;
//decalre Indexer .................
public int this[int index]
{
get
{
return arr[index];
}
set
{
arr[index] = value;
}

}
}
//use above Indexer on Button_Click
private void btnAdd_Click(object sender, EventArgs e)
{
IntCollection obj = new IntCollection();
obj[0] = 12;
obj[5]=78;
int res = obj[0];
MessageBox.Show("Values of index 0 is=" + res);
int res1 = obj[5];
MessageBox.Show("Values of index 5 is=" + res1);
}
}
}

Result

Above Concept without using any Indexers (Using two different Method)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IndexerConcept
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//create class which name is IntCollection
class IntCollection
{
private int[] arr = new int[100];
private int index = 0;
//create metthod for add no with AddNo method
public void AddNo(int no)
{
arr[index] = no;
index++;
}
//create metthod for get no with index
public int GetNo(int index)
{
return arr[index];
}
}
//call above method on button_Click
private void btnAdd_Click(object sender, EventArgs e)
{
IntCollection obj = new IntCollection();
obj.AddNo(12);
obj.AddNo(78);
int res = obj.GetNo(0);
MessageBox.Show("Values of index 0 is=" + res);
int res1 = obj.GetNo(1);
MessageBox.Show("Values of index 0 is=" + res1);
}
}
}

3 comments:

  1. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai | dot net training institute in velachery

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training and placement in chennai | dot net training institute in velachery

    ReplyDelete