C# need help parsing method in generic class

I am studying C# 3.0 syntax using O'Reilly's Pocket Reference on my own.  In the attached code, I am trying to call the "indexer" method as in:

public T this[int index] { get { return data[index]; } }

I am unsure how to parse this line.  I also need to know how to call it from Main.  The class code is attached.  Can anyone explain the line simply and let me know how to call it?

The error I am getting is: Using the generic type 'Generics.Stack<T>' requires '1' type arguments
//1.21.1. Generic Types: Here is a generic type Stack<T>, designed to stack instances of type T. 
            //Stack<T> declares a single generic parameter T:
 
            public class Stack<T>
            {
              //In our generic stack example, for instance, we could write an indexer that returns a generic item:
 
              public T this[int index] { get { return data[index]; } }
 
              int position;
              T[] data = new T[100];
              public void Push (T  obj) { data[position++] = obj;  }
              public T Pop()            { return data[--position]; }
            }

Open in new window

LVL 8
RGRodgersAsked:
Who is Participating?
 
philipjonathanConnect With a Mentor Commented:
Indexer means that you can access the internal data of an object as if that object is an array. Normally the said object itself contains an array of some sort, like in your example:

public T this[int index] { get { return data[index]; } }

This means that "index" is the position in the array that you want to access.

To use:
Console.WriteLine("Indexer = " + stack[0]);
// This will return you the first element in the stack (C# is 0-based)
0
 
aprestoCommented:
What line in your code gives you this error when you build?
0
 
RGRodgersAuthor Commented:
The line throwing the error is currently:
Console.WriteLine("Indexer = " + Stack.T());
I say currently because I have tried enough permutations to know that I don't know.
Thanks...Rick  
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
aprestoCommented:
Is this the example the book gave you? It looks as though you are missing the an index. Could you post your code for the declaration of your Stack instance, You are calling Stack as if it is declared as a static instance, but it is not
A useful article:
http://msdn.microsoft.com/en-us/library/3278tedw.aspx
0
 
RGRodgersAuthor Commented:
The book only gave me the class definition for this particular method.  I am trying to format the call myself.  So, if I am missing an index, that doesn't surprise me.  I'll attach the whole program.  I was trying to simplify it by not doing so but obviously complicated it instead.  Thanks for all your help...Rick
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
 
            //1.21.3. Generic Methods
            //A generic method declares generic parameters within the signature of a method.
 
            public class Swapper
            {
                public static void Swap<T>(ref T a, ref T b)
                {
                    T temp = a; a = b; b = temp;
 
                }
            }
 
            //1.21.1. Generic Types: Here is a generic type Stack<T>, designed to stack instances of type T. 
            //Stack<T> declares a single generic parameter T:
 
            public class Stack<T>
            {
              //In our generic stack example, for instance, we could write an indexer that returns a generic item:
 
              public T this[int index] { get { return data[index]; } }
 
              int position;
              T[] data = new T[100];
              public void Push (T  obj) { data[position++] = obj;  }
              public T Pop()            { return data[--position]; }
            }
 
    class Program
    {
        static void Main(string[] args)
        {
 
            //We can use Stack<T> as follows:
 
            Stack<int> stack = new Stack<int>();
            stack.Push(5);
            stack.Push(10);
            int x = stack.Pop();       // x is 10
            int y = stack.Pop();       // y is 5
            Console.WriteLine("x = " + x + ";y = " + y + ";");
 
 
            //Swap<T> can be used as follows:
 
            int a = 5, b = 10;
            Console.WriteLine("a = " + a + ";b = " + b + ";");
            Swapper.Swap (ref a, ref b);
            Console.WriteLine("a = " + a + ";b = " + b + ";");
 
            //Access generic indexer
            Console.WriteLine("Indexer = " + Stack.T());
        }
    }
}

Open in new window

0
 
RGRodgersAuthor Commented:
That makes just too much sense.  I think I got so far into trying to figure it out that I missed it.  Thanks for you help.  
0
 
RGRodgersAuthor Commented:
Thanks for the rapid and excellent responses.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.