Lets see the actual program in the book
Code:
// this program is the program on page 112 C#4 Wrox 27/11/2012
// it deals with the creation of a linkedlist------ we may remember this program.
//a n error came up as
//Error 1 The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments
// for the following code: "public class LinkedList<int> : IEnumerable<int>"
// so we included Systems.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace scratch_pad_2
{
class Program
{
static void Main(string[] args)
{
var list2 = new LinkedList<int>();
list2.AddLast(1);
list2.AddLast(3);
list2.AddLast(5);
foreach (int i in list2)
{
Console.WriteLine(i);
Console.Read();
}
}
}
public class LinkedListNode<int>
{
public LinkedListNode(int value)
{
this.Value = value;
}
public int Value { get; private set; }
public LinkedListNode<int> Next { get; private set; }
public LinkedListNode<int> Prev { get; private set; }
}
public class LinkedList<int>:IEnumerable<int>
{
public LinkedListNode<int> First { get; private set; }
public LinkedListNode<int> Last { get; private set; }
public LinkedListNode<int> AddLast(int node)
{
var newNode = new LinkedListNode<int>(node);
if (First == null)
{
First = newNode;
Last = First;
}
else
{
Last.Next = newNode;
Last = newNode;
}
return newNode;
}
public IEnumerator<int> GetEnumerator()
{
LinkedListNode<int> curren = First;
while (curren != null)
{
yield return curren.Value;
curren = curren.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
Error 1 Type parameter declaration must be an identifier not a type C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs 33 33 scratch pad 2
Error 2 Type parameter declaration must be an identifier not a type C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs 47 29 scratch pad 2
Error 1 Type parameter declaration must be an identifier not a type C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs 33 33 scratch pad 2
Error 2 Type parameter declaration must be an identifier not a type C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs 47 29 scratch pad 2
Q1 ) why these 2 errors come up? And what do they mean?
Q2 ) Can we use the following 2 directives simultaneously:-
using System.Collections.Generic;
using System.Collections;
Q3) why did we use
IEnumerator IEnumerable.GetEnumerator()
Instead of
Public IEnumerator GetEnumerator() { ...etc}, also because IEnumerable<T> inherits from IEnumerable
Q4) Why a data type of var has been selected in the following statement as the datatype of newNode:
var list2 = new LinkedList<int>();