Hello, please consider the code at the end of this posting, that depicts
simple case of custom enumerator. This particular example will enumerate
over list of random number within 1..10, and will stop once the value of 10
is reached. My question, is what's the real point of implemending the
Reset() method, if it is never used by foreach statement. Of course, it
gives me some allocated place where I can do some "enumerator resetting" and
call the Reset() method from the enumerator constructor, but it's somewhat
strange that the IEnumerator interface demands Reset() to be present, but
never actually cares to call it.
-- Pavils
using System;
using System.Collecti ons;
/*<Settings>
<Outfile>temp </Outfile>
<Run/>
</Settings>*/
class EnumTest : IEnumerable
{
class EnumTestEnumera tor : IEnumerator
{
private int rndValue;
private static Random rnd = new Random();
public object Current
{
get
{
Console.WriteLi ne("Current");
rndValue = rnd.Next(1, 11);
return rndValue;
}
}
public bool MoveNext()
{
Console.WriteLi ne("MoveNext()" );
return rndValue == 0 || rndValue != 10;
}
public void Reset()
{
Console.WriteLi ne("Reset()");
}
}
public IEnumerator GetEnumerator()
{
return new EnumTestEnumera tor();
}
}
class Tester
{
public static void Main()
{
EnumTest et = new EnumTest();
foreach (int value in et)
{
Console.WriteLi ne(value);
}
Console.Read();
}
} 2 2026
Pavils Jurjans <pa****@mailbox .riga.lv> wrote: Hello, please consider the code at the end of this posting, that depicts simple case of custom enumerator. This particular example will enumerate over list of random number within 1..10, and will stop once the value of 10 is reached. My question, is what's the real point of implemending the Reset() method, if it is never used by foreach statement.
The point is that IEnumerator isn't only used by foreach - anyone can
use it.
Of course, it gives me some allocated place where I can do some "enumerator resetting" and call the Reset() method from the enumerator constructor, but it's somewhat strange that the IEnumerator interface demands Reset() to be present, but never actually cares to call it.
It's up to the client to call it. If you know that you'll never call
it, don't bother implementing it fully - throw a NotSupportedExc eption,
for instance.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hallo Jon, The point is that IEnumerator isn't only used by foreach - anyone can use it.
Can you name some case, when it's used within default .NET framework
classes? That would help to understand better why the enumerator object is
being reused, not instantiated as new.
-- Pavils This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sasha |
last post by:
Hi,
I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and
all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the...
|
by: Scott M. |
last post by:
In a typical class, do I need to indicate that it implements the IDisposable
interface and then create a Dispose method that implements the Dispose
required by the IDisposable interface or can I just make a Sub Dispose() and
the CLR will know that this is the Dispose method to call when the object
falls out of scope?
Thanks.
|
by: Adam Clauss |
last post by:
I am developing an abstract class which implements IEnumerable<T>. I need
the actual implemented methods here to be abstract as well - they will be
implemented by MY subclasses.
However, I cannot seem to get the explicit interface method to accept being
abstract.
Take the following simple class:
abstract class TestEnumerable : IEnumerable<string>
|
by: KH |
last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made
my best guesses to no avail,, so help would be much appreciated!
public ref class T sealed
: public System::Collections::IEnumerable
, public System::Collections::Generic::IEnumerable<int>
{
// How to implement GetEnumerator() for both interfaces?
// Compiler complains that functions differ only in return type
// which I understand, but can't get the right...
|
by: Duncan |
last post by:
Hi,
I have been passed a class to work with, but am having some problems. I do
not know the origin of the code, and am having problems with some
functionality.
The VB class implements IEnumerable, which I understand allows for fwd only
movement through the custtom collection. This work fine and I am able to use
"For Each" loops without issue. However, the class has MoveFirst(),
MoveLast() etc methods within it, but these do not appear...
| |
by: Senthil |
last post by:
Hi ,
I am learning C# and now am stuck with a simple prorgam.Tried googling
but didn't get an answer :(. The following program gives me three
compilation errors.Can anyone enlighten me? Thanks.
using System;
using System.Collections.Generic;
using System.Text;
namespace Collections
|
by: Shikari Shambu |
last post by:
Hi,
I am trying to implement a collection that implements IEnumerable<T>. I
keep getting the following error
'IEnumerator<...>.Current' in explicit interface declaration is not a member
of interface. Please help me resolve the error
I was able to implement the non Generics version
|
by: Henri.Chinasque |
last post by:
I have a feeling this is a dumb one, but here it is:
IEnumerator<Timplements IDisposable, but the thing is I'm not sure
what I'm supposed to dispose! I'm also curious why IEnumerator<T>
implements IDisposable, but not IEnumerator.
Anyone?
Thanks,
HC
|
by: Giampaolo Rodola' |
last post by:
Hi,
I'm trying to implement an asynchronous scheduler for asyncore to call
functions at a later time without blocking the main loop.
The logic behind it consists in:
- adding the scheduled functions into a heapified list
- calling a "scheduler" function at every loop which checks the
scheduled functions due to expire soonest
Note that, by using a heap, the first element of the list is always
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |