Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 


C# Operators articles and tutorials

Read C# Operators articles

Sort by:

<< Start < Prev 1 Next > End >>
Page 1 of 1

C# Operators articles and tutorials

# articles: 1  


C# yield operator

View(s): 8902


C# yield operator


Prior to .NET 2.0, the only way you could create your own collection which could be traversed using the 'foreach' construct was by having you class implementing the IEnumerable and IEnumerator Interfaces.

However since .NET 2.0 we have a new operator called 'yield'. This allows your class to be iterated by its consumer but without the need to implement the above-mentioned interfaces.

Let us see a small example of how this works:

// Our class does not implement any interface !
public class School
{
    // Explicit initialization just for demo purposes !
    private string[] studentNames = {"John", "Joan","Joanne", "Tony", "Peter"};

    // The iterating method
    public IEnumerator GetEnumerator()
    {
        foreach (string name in studentNames)
        {
            // yield return in action !
            yield return name;
        }
    }
}

And now the code for the consumer:

class Program
{
    static void Main(string[] args)
    {
        School NorthCoast = new School();
        // How School's internal collection was iterated is hidden to the user
        foreach (string student in NorthCoast)
        {
  
(Continued...) View Full Aritlce


  Last updated on Wednesday, 02 April 2014
  Author: Mr. Ponna
4/5 stars (4 vote(s))

<< Start < Prev 1 Next > End >>
Page 1 of 1
Register Login Ask Us Write to Us Help