Hello all.
I have to implement IEnumerator interface in my (static) class. But
compilers throws me an error:
'GetEnumerator': cannot declare instance members in a static class
For example:
private static List<Product_productsList;
public static List<ProductProductsList
{
get
{
if (_productsList == null)
_productsList = new List<Product>();
return _productsList;
}
set { _productsList = value; }
}
public IEnumerator GetEnumerator()
{
foreach (Product p in _productsList)
{
yield return p;
}
}
That's why I have question: Can I implement Iterator with static
classes other way?
Thanks in advance 8 3392
dtarczynski wrote:
<snip>
That's why I have question: Can I implement Iterator with static
classes other way?
No. You can't implement any non-empty interface in a static class, as
an interface *insists* on certain instance members being present, and a
static class *prevents* any instance members being present.
Jon
Not really. The problem is that the foreach statement expects an instance
variable and not a type name. So, this code is illegal:
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp
{
static class EnumeratorTest
{
private static List<intm_IntList = new List<int>(new int[] { 0, 1,
2, 3, 4, 5, 6, 7, 8, 9 });
public static IEnumerator GetEnumerator()
{
foreach (int i in m_IntList)
yield return i;
}
}
class Program
{
static void Main(string[] args)
{
foreach (int i in EnumeratorTest)
Console.WriteLine(i);
}
}
}
This raises the following compiler error: "'ConsoleApp.EnumeratorTest' is
a 'type' but is used like a 'variable'"
However, you *can* add a ForEach method similar to the way it is done by
List<T>.ForEach(). For example:
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp
{
static class EnumeratorTest
{
private static List<intm_IntList = new List<int>(new int[] { 0, 1,
2, 3, 4, 5, 6, 7, 8, 9 });
public static void ForEach(Action<intaction)
{
foreach (int i in m_IntList)
action(i);
}
}
class Program
{
static void Main(string[] args)
{
EnumeratorTest.ForEach(delegate(int i) { Console.WriteLine(i); });
}
}
}
Best Regards,
Dustin Campbell
Developer Express Inc.
No. You can't implement any non-empty interface in a static class, as
an interface *insists* on certain instance members being present, and
a static class *prevents* any instance members being present.
You don't have to implement an interface to create a GetEnumerator() method
that uses an iterator. This code works fine:
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp
{
class EnumeratorTest
{
private List<intm_IntList = new List<int>(new int[] { 0, 1, 2, 3, 4,
5, 6, 7, 8, 9 });
public IEnumerator GetEnumerator()
{
foreach (int i in m_IntList)
yield return i;
}
}
class Program
{
static void Main(string[] args)
{
EnumeratorTest instance = new EnumeratorTest();
foreach (int i in instance)
Console.WriteLine(i);
}
}
}
Best Regards,
Dustin Campbell
Developer Express Inc.
There's a trick with static functions that return enumerators you can
use.
static class myStaticClass
{
static private object[] myIterable;
public static IEnumerable<objectMyStaticEnumerable() // a method
that returns an enumerable
{
foreach(object obj in myIterable)
{
yield return obj;
}
}
}
this can be used as follows:
foreach (obj in myStaticClass.MyStaticEnumerable())
{
...
}
I'm pretty sure that mess'll work, though I haven't tested it myself.
dtarczynski wrote:
Hello all.
I have to implement IEnumerator interface in my (static) class. But
compilers throws me an error:
'GetEnumerator': cannot declare instance members in a static class
For example:
private static List<Product_productsList;
public static List<ProductProductsList
{
get
{
if (_productsList == null)
_productsList = new List<Product>();
return _productsList;
}
set { _productsList = value; }
}
public IEnumerator GetEnumerator()
{
foreach (Product p in _productsList)
{
yield return p;
}
}
That's why I have question: Can I implement Iterator with static
classes other way?
Thanks in advance
Dustin Campbell <du*****@no-spam-pleasedevexpress.comwrote:
No. You can't implement any non-empty interface in a static class, as
an interface *insists* on certain instance members being present, and
a static class *prevents* any instance members being present.
You don't have to implement an interface to create a GetEnumerator() method
that uses an iterator.
That's true. You do, however, have to have an instance method - which
runs into exactly the same issue.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
That's true. You do, however, have to have an instance method - which
runs into exactly the same issue.
That is the problem. Of course, this begs the question why a static class
is needed in the first place. It seems counterintuitive for a collection
class to be static since you can't make it enumerable and you can't have
static indexers.
Best Regards,
Dustin Campbell
Developer Express Inc.
Either way, if static-like behaviour is needed, C# has easy idioms for
Singletons. That is, define an instance class, but make the
constructor private - and then give the class itself a static
constructor which in turn constructs the instance object and stores it
in a public static field. Alternately, one can use a property and make
it lazily instantiated, rather than using the static constructor to
construct the single instance object.
Still, I prefer just using a generator to include a method that returns
an IEnumerable if static semantics are desired.
Dustin Campbell wrote:
That's true. You do, however, have to have an instance method - which
runs into exactly the same issue.
That is the problem. Of course, this begs the question why a static class
is needed in the first place. It seems counterintuitive for a collection
class to be static since you can't make it enumerable and you can't have
static indexers.
Best Regards,
Dustin Campbell
Developer Express Inc.
Either way, if static-like behaviour is needed, C# has easy idioms for
Singletons. That is, define an instance class, but make the
constructor private - and then give the class itself a static
constructor which in turn constructs the instance object and stores it
in a public static field. Alternately, one can use a property and
make it lazily instantiated, rather than using the static constructor
to construct the single instance object.
Still, I prefer just using a generator to include a method that
returns an IEnumerable if static semantics are desired.
Sure, or even the old System.Collections.ICollection interface.
Best Regards,
Dustin Campbell
Developer Express Inc. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gregory Bond |
last post by:
I'm trying to extend Python with an iterator class that should be
returned from a factory function.
For some reason the iterator object is not being properly initialised if
the iterator is...
|
by: Alexander Stippler |
last post by:
Hi,
I have to design some two dimensional iterators and I'm not quite sure about
the design. I'd like to have the iterators mostly STL-like. The STL does
not contain two dimensional iterators, I...
|
by: richard.forrest1 |
last post by:
I have a problem with an abstract interface class whose implementation
classes need to return different iterator types (but with the same
value_types etc).
Classes A and B both conform to the...
|
by: Alvin |
last post by:
Will it be possible to have more than one Iterator in a type?
|
by: Kamen Yotov |
last post by:
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...
|
by: Jiri Kripac |
last post by:
Languages such as Simula 67 contain a general concept of coroutines that
allow the execution of a method to be suspended without rolling back the
stack and then later resumed at the same place as...
|
by: Andrew Matthews |
last post by:
Hi All,
I have the following little class of iterators that allow me to iterate over
elements in the file system. I have nested some of them, and then added Func<FileInfo,
booldelegates to filter...
|
by: gexarchakos |
last post by:
Hi there,
Please give me at least a hint...
I have a problem implementing a function object with parameters two
iterators. That is:
A class 'node' produces messages using a routing policy....
|
by: Jess |
last post by:
Hello,
Iterators are typically put into five different categories, namely
input iterator, output iterator, forward iterator, bidirectional
iterator and random iterator. The differences come...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |