Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

yield in c#


public void Consumer()
{
    foreach(int i in Integers())
    {
        Console.WriteLine(i.ToString());
    }
}

public IEnumerable<int> Integers()
{
    yield return 1;
    yield return 2;
    yield return 4;
    yield return 8;
    yield return 16;
    yield return 16777216;
}

Comment

yield c#

IEnumerable<char> hello()
{
	foreach (char ch in "hello world")
	{
		yield return ch;
	}
}
Console.WriteLine(hello());
//Submission#0+<hello>d__0                                                                                                                             
foreach(char ch in hello())
{
	Console.WriteLine(ch);
}
// h                                                                                                                                                    
// e                                                                                                                                                    
// l                                                                                                                                                    
// l                                                                                                                                                    
// o                                                                                                                                                    
//	
// w                                                                                                                                                    
// o                                                                                                                                                    
// r                                                                                                                                                    
// l                                                                                                                                                    
// d 
Comment

c# yield keyword

class Program
{
    static int[,] _grid = new int[15, 15];
    
    static void Main()
    {
        // Initialize some elements in 2D array.
        _grid[0, 1] = 4;
        _grid[4, 4] = 5;
        _grid[14, 2] = 3;
        
        // Sum values in 2D array.
        int sum = 0;
        foreach (int value in GridValues())
        {
            sum += value;
        }
        // Write result.
        Console.WriteLine("SUMMED 2D ELEMENTS: " + sum);
    }
    
    public static IEnumerable<int> GridValues()
    {
        // Use yield return to return all 2D array elements.
        for (int x = 0; x < 15; x++)
        {
            for (int y = 0; y < 15; y++)
            {
                yield return _grid[x, y];
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# xml get child node by name 
Csharp :: scale between tow ranges c# 
Csharp :: how to pass id from view to controller in asp.net core 
Csharp :: .net 6 autofac 
Csharp :: c# replace multiple characters 
Csharp :: unity reset random seed 
Csharp :: how to remove all comma from string c# 
Csharp :: Raycasting to find mouseclick on Object in unity 2d games 
Csharp :: how to reload app.config file at runtime in c# 
Csharp :: c# xml comment type reference 
Csharp :: c# normalize value 
Csharp :: C# api get value from header 
Csharp :: get connection string from web.config in c# 
Csharp :: c# remove everything after last slash 
Csharp :: Unity rainbow color changing object 
Csharp :: c# return tuple 
Csharp :: c# loop string 
Csharp :: unity singleton 
Csharp :: serilog .net 6 
Csharp :: c# destroy function...unity 
Csharp :: unity audio source 
Csharp :: c# mvc get current directory 
Csharp :: c# check characters in string 
Csharp :: c# read excel file using epplus save to datatable 
Csharp :: unity pickup and drop objects 
Csharp :: wpf textbox insert text at caret position 
Csharp :: Rotating an object in Unity usign Physics 
Csharp :: linq select max value from list 
Csharp :: How to decode Microsoft Local token in service 
Csharp :: c# if string in licbox 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =