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 :: entity framework delete record with foreign key constraint 
Csharp :: unity reset random seed 
Csharp :: c# how to call methods from another class 
Csharp :: c# system.text.json deserialize 
Csharp :: Get Last Access Time Of Directory C# 
Csharp :: which game engine is best 
Csharp :: Print arraylist values to console unity 
Csharp :: c# xml comment type reference 
Csharp :: how to generate a random number in c# 
Csharp :: get domain name from email in asp.net c# 
Csharp :: c# combobox lock edit 
Csharp :: c# how to compare 2 dates without time 
Csharp :: what is list in c# 
Csharp :: c# remove time in datetime 
Csharp :: get ad user using email address microsoft graph C# 
Csharp :: listbox items to string c# 
Csharp :: unity detect when an object has been clicked 
Csharp :: unity ui button 
Csharp :: update table in C# 
Csharp :: remove duplicates in the list using linq 
Csharp :: C# Find first thing on a list 
Csharp :: for statement syntax C sharp 
Csharp :: c# remove invalid directory characters 
Csharp :: C# Blocks with statements 
Csharp :: c# merge two lists as queryable 
Csharp :: how to subtract two dates in dart 
Csharp :: get script directory c# 
Csharp :: how to check if an integer is in array c# 
Csharp :: how to download somthing from one drive unity 
Csharp :: regex only letters and numbers c# 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =