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

OOPS-Constructors and distractor

Hi friends today we will discussed about constructors and distractor in csharp Constructors are used to memory allocation for real world entities which are created with the help of objects. When program or application is running, application required memory to save and manipulate data of that real world object/entities. Now the question is how much memory is required for that object? Frankly no one can decide how much memory is required for that application because of that all object oriented programming languages depends on heap memory. Heap memory is a logical memory in RAM (temporary memory) and size is logically infinity (physically it is limited).This memory has to be creating at run time when we are using object in our program/application. Now we have to know that what type of data members we have in object. In object we have two types of data members’ object instance  shared instance Object instance are allocated occupies object memory and eve

FAQS PART 3

data integrity depends on data sharing accuracy  redundency 2&4                              ANS: accuracy which is lightweight protocal?         ANS:soap class is which based memory      ANS: Heap  csharp code is                          ANS: managed sn.exe generates what?             ANS: Strong Name file and fileinfo classes doesn't offer the set of functionality moving creating copying  deleting                                   ANS: Creating datareader field count return type        ANS: integer type dataset and untype dataset difference? type dataset have schema information of database(primary key and FK info) untype does not have schema information GOALS OF INTEROPERABILITY ANSWER net does not affect existing COM objects .Net and COM components interoperate with each other COM components can be upgraded into .NET components if u build the application what file with extension generated                   ANS: .exe null is a string ?