none
cant i add a class and use this class in current class RRS feed

  • Question

  • ok, my question is kinda hard to express, I'm sorry if i make it kinda badly...
    I have a doubt concerning lists: Lists are so we can make variable sized arrays right? 

    And another thing, I copied and pasted the following code in one of the msdn c# tutorials about lists, and i have a doubt concerning this:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace programx
    {
        public class GenericList<T>
        {
            // The nested class is also generic on T. 
            private class Node
            {
                // T used in non-generic constructor. 
                public Node(T t)
                {
                    next = null;
                    data = t;
                }
    
                private Node next;
                public Node Next
                {
                    get { return next; }
                    set { next = value; }
                }
    
                // T as private member data type. 
                private T data;
    
                // T as return type of property. 
                public T Data
                {
                    get { return data; }
                    set { data = value; }
                }
            }
    
            private Node head;
    
            // constructor 
            public GenericList()
            {
                head = null;
            }
    
            // T as method parameter type: 
            public void AddHead(T t)
            {
                Node n = new Node(t);
                n.Next = head;
                head = n;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                Node current = head;
    
                while (current != null)
                {
                    yield return current.Data;
                    current = current.Next;
                }
            }
        }
        class TestGenericList
        {
            static void Main()
            {
                // int is the type argument
                GenericList<int> list = new GenericList<int>();
    
                for (int x = 0; x < 10; x++)
                {
                    list.AddHead(x);
                }
    
                foreach (int i in list)
                {
                    System.Console.Write(i + " ");
                }
                System.Console.WriteLine("\nDone");
                System.Console.ReadLine();
    
            }
        }
    
    }
    
    I dont know what a few things means..
    What is this 
     public Node(T t)
                {
                    next = null;
                    data = t;
                }
    What is the purpose of this T and t? and what does this next=null makes? data=t as well, i dont understand its purpose..

    I think asking these noob basic stuff here could not be such a good idea, i think it may be better to learn it reading something and banging on it until i learn it, but the thing is i dont know any source that teach this from the start.. 

    Im sorry, but can someone explain this to me please? I know its a lot  of doubts but id like to learn and get rid of it.
    Thanks
    Friday, September 28, 2012 10:26 AM

Answers

  • Hi,

    I would suggest to start with some C# tutorials so you get a soft start where step by step is explained.

    What you have in your given code is:

    a) A class GenericList<T> is defined (Where T is a type the user of the class can define)

    b) inside that class a class Node is defined. (private class Node { ... } )

    c) the class Node has a constructor which takes an argument t of Type T.

    d) inside the constructor, the field Next is set to null and the field data is set to t (the argument handed to the constructor).

    That was the explanation that you asked for.

    With kind regards,

    Konrad

    • Proposed as answer by RohitArora Friday, September 28, 2012 4:41 PM
    • Marked as answer by Vouschrou Saturday, September 29, 2012 11:56 PM
    Friday, September 28, 2012 10:39 AM