By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Whidbey C# Generics/Iterators Question

P: n/a
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);
}
}
}
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You should post this to the microsoft.beta.whidbey.generics newsgroup, not
to the public csharp group.

Bruno.

"Kamen Yotov" <ka***@yotov.org> a écrit dans le message de
news:e7**************@tk2msftngp13.phx.gbl...
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);
}
}
}

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.