There is this piece of code that I need to follow:
For easy understanding I posted the queries in the next thread, so that , if anyone would
may directly go to the questions----------
using System;
using System.Collections;
namespace Wrox.ProCSharp.Arrays
{
public class HelloCollection
{
public IEnumerator < string > GetEnumerator()
{
yield return "Hello";
yield return "World";
}
}
===============
public void HelloWorld()
{
var helloCollection = new HelloCollection();
foreach (var s in helloCollection)
{
Console.WriteLine(s);
}
}
}
====================
public class HelloCollection
{
public IEnumerator GetEnumerator()
{
return new Enumerator(0);
}
public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable
{
private int state;
private string current;
public Enumerator(int state)
{
this.state = state;
}
bool System.Collections.IEnumerator.MoveNext()
{
switch (state)
{
case 0:
current = "Hello";
state = 1;
return true;
case 1:
current = "World";
state = 2;
return true;
case 2:
break;
}
return false;
}
void System.Collections.IEnumerator.Reset()
{
throw new NotSupportedException();
}
string System.Collections.Generic.IEnumerator<string>.Current
{
get
{
return current;
}
}
object System.Collections.IEnumerator.Current
{
get
{
return current;
}
}
void IDisposable.Dispose()
{
}
}
}
Q1 ] What does the following statement, in the following code mean:-
return new Enumerator(0);
q2 ] Moreover when do we need to use a nested class, as it has been used here?
q3 ] If we note the inheritence below , in the statement
public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable
a generic IEnumerator<string> and a non-generic IEnumerator are simultaneously inherited
----------- When do we need to use such a thing that a generic and a non-generic are both
a part of an inheritence.
1) It creates an instance of the class Enumerator (new Enumerator) by passing zero to a matching constructor ((0)) and returns the created instance
2) A nested class has access to the private members of its enclosing class. All access modifiers are available, not just public and internal.
3) Don't think of it as inheritance when it is an interface (after all there is nothing to inherit), think of it as a promise to fulfill a contract. The class Enumerator says if you want a type that satisifies the IEnumerator<string> contract then I can
do it. If you want a type that can satisfy the IEnumerator contract then I can do it. If you want a type that can satisfy the IDisposable contract then I can do it.. Different clients may want different services and this declaration tells clients what services
the class Enumerator can provide.
It seems very, very unlikely to me that this is a good idea. Yes, you can put them in separate namespaces but you can also throw yourself in front of a train. Just because you can do something does not make it a good idea.
Why do you have two classes with the same name? Are they different? If they are different then why have the same name? If they are not different then it what sense do you have two classes?
Obviously the code does not exist because you wouldn't have asked how to do it if you had already done it. But what makes you think that you need two classes with the same name? What is the business problem that you think this will solve?
amigo 1
Member
60 Points
199 Posts
enumerations-- about
Aug 18, 2012 05:45 AM|LINK
There is this piece of code that I need to follow:
For easy understanding I posted the queries in the next thread, so that , if anyone would
may directly go to the questions----------
using System;
using System.Collections;
namespace Wrox.ProCSharp.Arrays
{
public class HelloCollection
{
public IEnumerator < string > GetEnumerator()
{
yield return "Hello";
yield return "World";
}
}
===============
public void HelloWorld()
{
var helloCollection = new HelloCollection();
foreach (var s in helloCollection)
{
Console.WriteLine(s);
}
}
}
====================
public class HelloCollection
{
public IEnumerator GetEnumerator()
{
return new Enumerator(0);
}
public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable
{
private int state;
private string current;
public Enumerator(int state)
{
this.state = state;
}
bool System.Collections.IEnumerator.MoveNext()
{
switch (state)
{
case 0:
current = "Hello";
state = 1;
return true;
case 1:
current = "World";
state = 2;
return true;
case 2:
break;
}
return false;
}
void System.Collections.IEnumerator.Reset()
{
throw new NotSupportedException();
}
string System.Collections.Generic.IEnumerator<string>.Current
{
get
{
return current;
}
}
object System.Collections.IEnumerator.Current
{
get
{
return current;
}
}
void IDisposable.Dispose()
{
}
}
}
amigo 1
Member
60 Points
199 Posts
Re: enumerations-- about
Aug 18, 2012 05:46 AM|LINK
Q1 ] What does the following statement, in the following code mean:-
return new Enumerator(0);
q2 ] Moreover when do we need to use a nested class, as it has been used here?
q3 ] If we note the inheritence below , in the statement
public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable
a generic IEnumerator<string> and a non-generic IEnumerator are simultaneously inherited
----------- When do we need to use such a thing that a generic and a non-generic are both
a part of an inheritence.
Paul Linton
Star
13431 Points
2535 Posts
Re: enumerations-- about
Aug 18, 2012 10:09 PM|LINK
1) It creates an instance of the class Enumerator (new Enumerator) by passing zero to a matching constructor ((0)) and returns the created instance
2) A nested class has access to the private members of its enclosing class. All access modifiers are available, not just public and internal.
3) Don't think of it as inheritance when it is an interface (after all there is nothing to inherit), think of it as a promise to fulfill a contract. The class Enumerator says if you want a type that satisifies the IEnumerator<string> contract then I can do it. If you want a type that can satisfy the IEnumerator contract then I can do it. If you want a type that can satisfy the IDisposable contract then I can do it.. Different clients may want different services and this declaration tells clients what services the class Enumerator can provide.
amigo 1
Member
60 Points
199 Posts
Re: enumerations-- about
Aug 20, 2012 05:49 AM|LINK
thank you teacher
how are U doing today
one more thing
there are 2 HelloCollections classes
How may we have 2 classes with the same name?
Mark - MSFT
Contributor
7071 Points
435 Posts
Microsoft
Re: enumerations-- about
Aug 20, 2012 07:25 AM|LINK
Hi amigo,
Use different namespace, then classes can with same name .
Like this
namespace NameSpace1 { public class HelloCollections { } }and
namespace NameSpace2 { public class HelloCollections { } }Declare new object
Thanks
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Paul Linton
Star
13431 Points
2535 Posts
Re: enumerations-- about
Aug 20, 2012 09:12 AM|LINK
It seems very, very unlikely to me that this is a good idea. Yes, you can put them in separate namespaces but you can also throw yourself in front of a train. Just because you can do something does not make it a good idea.
Why do you have two classes with the same name? Are they different? If they are different then why have the same name? If they are not different then it what sense do you have two classes?
Obviously the code does not exist because you wouldn't have asked how to do it if you had already done it. But what makes you think that you need two classes with the same name? What is the business problem that you think this will solve?
amigo 1
Member
60 Points
199 Posts
Re: enumerations-- about
Aug 21, 2012 12:58 PM|LINK
yes you are right
actually this code is taken from a book called
;-
Professional C# 4 from Wrox (Willey)
unfortunately , teacher , a book comming from such a house , has problems too , I see -- in fact many
amigo 1
Member
60 Points
199 Posts
Re: enumerations-- about
Sep 06, 2012 07:20 AM|LINK
I am Sorry
Actually I was wrong
Wrox Books simply rocks they are really good
in fact the above 2 blocks are those which we write