Connecting Tech Pros Worldwide Forums | Help | Site Map

Whidbey C# Generics/Iterators Question

Kamen Yotov
Guest
 
Posts: n/a
#1: Nov 15 '05
Hello,
I got my hands on the PDC preview version of whidbey and I think I managed
to break the compiler/runtime with my first Generics/Iterators attempt. The
reason I am posting it here is that I might be wrong, in which case, please
correct me!

The following program prints garbade instead the intended 5,6,1. I think it
is printing the integer values of the pointers which represent some
references... Any ideas?

using System;
using System.Collections.Generic;

namespace test2
{
class List<T>: IEnumerable<T>, IEnumerable<ListNode<T>>
{
protected ListNode<T> _head;
protected ListNode<T> _last;

public List ()
{
}

public void Add (T _value)
{
if (_head == null)
_head = _last = new ListNode<T>(_value, null);
else
_last = _last.Next = new ListNode<T>(_value, null);
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
for (ListNode<T> _current = _head; _current != null; _current =
_current.Next)
yield _current.Value;

/*
foreach (ListNode<T> n in (IEnumerable<ListNode<T>>)this)
yield n.Value;
*/
}

IEnumerator<ListNode<T>> IEnumerable<ListNode<T>>.GetEnumerator()
{
for (ListNode<T> _current = _head; _current != null; _current =
_current.Next)
yield _current;
}
}

class ListNode<T>
{
protected T _value;
protected ListNode<T> _next;

public ListNode (T _value, ListNode<T> _next)
{
Value = _value;
Next = _next;
}

public T Value
{
get
{
return _value;
}
set
{
_value = value;
}
}

public ListNode<T> Next
{
get
{
return _next;
}
set
{
_next = value;
}
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
List<int> l = new List<int>();

l.Add(5);
l.Add(6);
l.Add(1);

foreach (int q in (IEnumerable<int>)l)
Console.WriteLine(q);
}
}
}



Bruno Jouhier [MVP]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Whidbey C# Generics/Iterators Question


You should post this to the microsoft.beta.whidbey.generics newsgroup, not
to the public csharp group.

Bruno.

"Kamen Yotov" <kamen@yotov.org> a écrit dans le message de
news:e7X0kTh7DHA.4060@tk2msftngp13.phx.gbl...[color=blue]
> Hello,
> I got my hands on the PDC preview version of whidbey and I think I managed
> to break the compiler/runtime with my first Generics/Iterators attempt.[/color]
The[color=blue]
> reason I am posting it here is that I might be wrong, in which case,[/color]
please[color=blue]
> correct me!
>
> The following program prints garbade instead the intended 5,6,1. I think[/color]
it[color=blue]
> is printing the integer values of the pointers which represent some
> references... Any ideas?
>
> using System;
> using System.Collections.Generic;
>
> namespace test2
> {
> class List<T>: IEnumerable<T>, IEnumerable<ListNode<T>>
> {
> protected ListNode<T> _head;
> protected ListNode<T> _last;
>
> public List ()
> {
> }
>
> public void Add (T _value)
> {
> if (_head == null)
> _head = _last = new ListNode<T>(_value, null);
> else
> _last = _last.Next = new ListNode<T>(_value, null);
> }
>
> IEnumerator<T> IEnumerable<T>.GetEnumerator()
> {
> for (ListNode<T> _current = _head; _current != null; _current =
> _current.Next)
> yield _current.Value;
>
> /*
> foreach (ListNode<T> n in (IEnumerable<ListNode<T>>)this)
> yield n.Value;
> */
> }
>
> IEnumerator<ListNode<T>> IEnumerable<ListNode<T>>.GetEnumerator()
> {
> for (ListNode<T> _current = _head; _current != null; _current =
> _current.Next)
> yield _current;
> }
> }
>
> class ListNode<T>
> {
> protected T _value;
> protected ListNode<T> _next;
>
> public ListNode (T _value, ListNode<T> _next)
> {
> Value = _value;
> Next = _next;
> }
>
> public T Value
> {
> get
> {
> return _value;
> }
> set
> {
> _value = value;
> }
> }
>
> public ListNode<T> Next
> {
> get
> {
> return _next;
> }
> set
> {
> _next = value;
> }
> }
> }
>
> class Class1
> {
> [STAThread]
> static void Main(string[] args)
> {
> List<int> l = new List<int>();
>
> l.Add(5);
> l.Add(6);
> l.Add(1);
>
> foreach (int q in (IEnumerable<int>)l)
> Console.WriteLine(q);
> }
> }
> }
>
>[/color]


Closed Thread