Skip to main content

Iterator in csharp

Iterator


What is iterator?
Iterator is a method or function implementation which implements the iterative features for objects and its return type must be IEnumerable type.
We can implement iterators in C# read-only property also (only with get )
In each iteration this method or properties returns one element with the help of Yield return statement
What is yield
Yield is a keyword which is introduced in Csharp and VB.Net to implement iterator .yield is working like return statement in functions but it will call more than one time like loop.
yield gives enumeration properties implicitly for object(i.e means explicitly no need to implement IEnumerator) .
Yield was implemented in C-sharp and vb.net with different keyword
Yield return <expression>; in C#
We used yield return statement in iterator to return each element

Example

static void Main(string[] args)
        {
            foreach (var item in getno())
            {
                Console.WriteLine(item);
               
            }
        }
        public static IEnumerable<string> getno()
        {
            yield return "2";
            yield return "10";
            yield return "120";
            yield return "102";
        }





Can I use yield statement in exception try catch block?
By Microsoft definition
Yield can be placed in try-finally block, but cannot be in try-catch.
Yield have another key word break, it  is used to break  present iteration of iterator



Example
public class Employeelist
    {
        public System.Collections.Generic.IEnumerable<Employee> Nexemployee
        {
            get
            {
                yield return new Employee { Name = "elan", Experience = 12 };
                yield return new Employee { Name = "bond", Experience = 25 };
                yield return new Employee { Name = "larry ", Experience = 2, };
                yield return new Employee { Name = "amanda", Experience = 3 };
            }
        }
    }

    public class Employee
    {
        public String Name { get; set; }

        public int Experience { get; set; }
    }

    class Program
    {
      

        static void Main(string[] args)
        {
            var theemp = new Employeelist();
            foreach (Employee empitem in theemp.Nexemployee)
                Console.WriteLine(empitem.Name + " " + empitem.Experience.ToString());
        }
    }

Reference
http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx


Comments

Popular posts from this blog

Shortcut keys inMSDOTNET 2015

when ever we want work with MS.net we have to learn about some keys which can help to improve our work force in development area .i  got some shortcut key in msdn ,You can view all of the keyboard shortcuts for Visual Web Developer. we can create our own shortcuts by doing the following:  on the  Tools  -->  Options ; it will open dialog box  of the Options ,  select  Environment  and then  Keyboard . we have to select the  Show all settings  check box in order to view the  Keyboard  setting. Shortcut Description CTRL+B Switches the selected text between bold and normal. CTRL+I Switches the font style of the selected text between italic and roman. CTRL+U Switches the font style of the selected text between underline and roman. CTRL+SHIFT+L Displays the  Bookmark  dialog box. CTRL+L When text is selected, displays the  Hyperlink  dialog box. ...

Fundamentals of Design Patterns

Fundamentals of Design Patterns   hi developers and programmers  in this post i am going to share Videos where i learned fundamentals of design patterns   it is great visual you can enjoy this  have a nice day