Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IENumerable and yield examples:
- //1:
- public class PowersOf2
- {
- static void Main()
- {
- // Display powers of 2 up to the exponent of 8:
- foreach (int i in Power(2, 8))
- {
- Console.Write("{0} ", i);
- }
- }
- public static System.Collections.Generic.IEnumerable<int> Power(int number, int exponent)
- {
- int result = 1;
- for (int i = 0; i < exponent; i++)
- {
- result = result * number;
- yield return result;
- }
- }
- // Output: 2 4 8 16 32 64 128 256
- }
- //Example 2:
- public static class GalaxyClass
- {
- public static void ShowGalaxies()
- {
- var theGalaxies = new Galaxies();
- foreach (Galaxy theGalaxy in theGalaxies.NextGalaxy)
- {
- Debug.WriteLine(theGalaxy.Name + " " + theGalaxy.MegaLightYears.ToString());
- }
- }
- public class Galaxies
- {
- public System.Collections.Generic.IEnumerable<Galaxy> NextGalaxy
- {
- get
- {
- yield return new Galaxy { Name = "Tadpole", MegaLightYears = 400 };
- yield return new Galaxy { Name = "Pinwheel", MegaLightYears = 25 };
- yield return new Galaxy { Name = "Milky Way", MegaLightYears = 0 };
- yield return new Galaxy { Name = "Andromeda", MegaLightYears = 3 };
- }
- }
- }
- public class Galaxy
- {
- public String Name { get; set; }
- public int MegaLightYears { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement