Suresh Kumar
Suresh Kumar

Reputation: 1

diplaying data in private array by using indexers in c#

I am declaring two private arrays in indexes and displaying the data in main. However, it is not displaying any one tell me how to display the data in the two private arrays in indexers?

class Program
{
    static void Main(string[] args)
    {

        student sc = new student();
        for (int i = 0; i < sc.mlength; i++)
        {
            Console.WriteLine(sc[i]);
        }
        Console.ReadLine();
      //i am declaring two private arrays in indexes and displaying the data in main is not displaying any one tell me how to display the data in the two private arrays in indexers?  
    }
}
public class student
{
    private int[] _marks = new int[] { 60, 68, 70 };
    private string[] _names = new string[] { "suri", "kumar", "suresh" };
    public int this[int i]
    {
        get
        {
            return _marks[i];
        }
        set
        {
            _marks[i] = value;
        }

    }
    public string this[int i]
    {
        get
        {
            return _names[Convert.ToInt32(i)];
        }
        set
        {
            _names[Convert.ToInt32(i)] = value;
        }
    }
    public int mlength
    {
        get
        {
            return _marks.Length;
        }
    }
    public int nlenght
    {
        get
        {
            return _names.Length;
        }
    }
}

}

Upvotes: 0

Views: 382

Answers (1)

Thennarasan
Thennarasan

Reputation: 718

Indexers allow 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. These objects could be a finite set of class members, another array, or some complex data structure. Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers. Here’s an example.

Example:

using System;

class IntIndexer
{
private string[] myData;

public IntIndexer(int size)
{
    myData = new string[size];

    for (int i=0; i < size; i++)
    {
        myData[i] = "empty";
    }
}

public string this[int pos]
{
    get
   {
        return myData[pos];
    }
    set
   {
        myData[pos] = value;
    }
}

static void Main(string[] args)
{
    int size = 10;

    IntIndexer myInd = new IntIndexer(size);

    myInd[9] = "Some Value";
    myInd[3] = "Another Value";
    myInd[5] = "Any Value";

    Console.WriteLine("\nIndexer Output\n");

    for (int i=0; i < size; i++)
    {
        Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
    }
}
}

The IntIndexer class has a string array named myData. This is a private array that external users can’t see. This array is initialized in the constructor, which accepts an int size parameter, instantiates the myData array, and then fills each element with the word “empty”.

The IntIndexer class has a string array named myData. This is a private array that external users can’t see. This array is initialized in the constructor, which accepts an int size parameter, instantiates the myData array, and then fills each element with the word “empty”.

The next class member is the Indexer, which is identified by the this keyword and square brackets, this[int pos]. It accepts a single position parameter, pos. As you may have already guessed, the implementation of an Indexer is the same as a Property. It has get and setaccessors that are used exactly like those in a Property. This indexer returns a string, as indicated by the string return value in the Indexer declaration.

The Main() method simply instantiates a new IntIndexer object, adds some values, and prints the results. Here’s the output:

Indexer Output

myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value

Upvotes: 1

Related Questions