T
Tony Johansson
Hello!
I have a generic BinaryTree called Tree<T> below where I have implemented
the IEnumerable<T> interface.
The class header is shown below.
public class Tree<T> : IEnumerable<T> where T : IComparable<T>
{
....
....
}
I have also another class called TreeEnumerator<T> where the interface
IEnumerator<T> has been implemented.
The header is shown below.
class TreeEnumerator<T> : IEnumerator<T> where T : IComparable<T>
{
....
...
}
When VS generate the skeleton for the interface implementation IEnumerable
in class Tree<T>
it using this type of construction.
IEnumerator IEnumerable.GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
I have also a Testclass to test the Tree<T> and TreeEnumerator<T>.
Now to my question.
I noticed in the Testclass that method GetEnumerator was not shown in the
Intelligens
for example if I did the following Tree<int> myTree = new Tree<int>(12);
and wrote myTree. no GetEnumerator was shown.
Why ?
If I write System.Collections.IEnumerator iter = myTree.GetEnumerator();
I get compile error saying no definition for GetEnumerator.
So what access is it on the GetEnumerator ?
I then changed the GetEnumerator in the Tree<T> class to this instead
public IEnumerator GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
Now I can for example do System.Collections.IEnumerator iter =
myTree.GetEnumerator();
and intelligens shows GetEnumerator()
So as a summary I want to have an answer on two questions.
Question 1. What access is it on the GetEnumerator() when having this
construction
IEnumerator IEnumerable.GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
Question 2. What is the recommended way to implement is it to use
IEnumerator IEnumerable.GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
which mean that direct access to GetEnumerator is not allowed like
System.Collections.IEnumerator iter = myTree.GetEnumerator();
or writing
public IEnumerator GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
I know that is some cases you have no choice you must use the explicit
implementation
but in those cases when you can choose which one do you recommend ?. I thing
that using this one is better
public IEnumerator GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
because I can use for example this construction
System.Collections.IEnumerator iter = myTree.GetEnumerator();
and Intelligens work with GetEnumerator
PS A long posting hope you understand what I mean.
//Tony
I have a generic BinaryTree called Tree<T> below where I have implemented
the IEnumerable<T> interface.
The class header is shown below.
public class Tree<T> : IEnumerable<T> where T : IComparable<T>
{
....
....
}
I have also another class called TreeEnumerator<T> where the interface
IEnumerator<T> has been implemented.
The header is shown below.
class TreeEnumerator<T> : IEnumerator<T> where T : IComparable<T>
{
....
...
}
When VS generate the skeleton for the interface implementation IEnumerable
in class Tree<T>
it using this type of construction.
IEnumerator IEnumerable.GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
I have also a Testclass to test the Tree<T> and TreeEnumerator<T>.
Now to my question.
I noticed in the Testclass that method GetEnumerator was not shown in the
Intelligens
for example if I did the following Tree<int> myTree = new Tree<int>(12);
and wrote myTree. no GetEnumerator was shown.
Why ?
If I write System.Collections.IEnumerator iter = myTree.GetEnumerator();
I get compile error saying no definition for GetEnumerator.
So what access is it on the GetEnumerator ?
I then changed the GetEnumerator in the Tree<T> class to this instead
public IEnumerator GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
Now I can for example do System.Collections.IEnumerator iter =
myTree.GetEnumerator();
and intelligens shows GetEnumerator()
So as a summary I want to have an answer on two questions.
Question 1. What access is it on the GetEnumerator() when having this
construction
IEnumerator IEnumerable.GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
Question 2. What is the recommended way to implement is it to use
IEnumerator IEnumerable.GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
which mean that direct access to GetEnumerator is not allowed like
System.Collections.IEnumerator iter = myTree.GetEnumerator();
or writing
public IEnumerator GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
I know that is some cases you have no choice you must use the explicit
implementation
but in those cases when you can choose which one do you recommend ?. I thing
that using this one is better
public IEnumerator GetEnumerator()
{
return new TreeEnumerator<T>(this);
}
because I can use for example this construction
System.Collections.IEnumerator iter = myTree.GetEnumerator();
and Intelligens work with GetEnumerator
PS A long posting hope you understand what I mean.
//Tony