Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

What is the yield keyword used for 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

how to use yield in 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 :: what is void onmousedown() 
Csharp :: multidimensional arrays c# 
Csharp :: c# get classes which inherits 
Csharp :: Sort ListBox numerically in C# 
Csharp :: c# wpf timer 
Csharp :: type or namespace text could not be found unity 
Csharp :: compact in laravrl 
Csharp :: linq from multiple tables 
Csharp :: difference between awake and start unity 
Csharp :: how to use yield in c# 
Csharp :: c# tostring decimal 2 places 
Csharp :: C# round number of digits after decimal point 
Csharp :: how to close another app in system with c# 
Csharp :: if statement 
Csharp :: unity public static variable 
Csharp :: C# Switch and case 
Csharp :: iterate though data in firebase unity 
Csharp :: linq string comparison case insensitive 
Csharp :: Failed to generate swagger file. Error dotnet swagger tofile --serializeasv2 --output 
Csharp :: interface property implementation c# 
Csharp :: c# method returns multiple values 
Csharp :: audiosource unity 
Csharp :: password regex asp.net 
Csharp :: c# setting window size 
Csharp :: c# generate random int list 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: how to check type in c# 
Csharp :: unity rotate around point 
Csharp :: unity rigidbody freeze rotation y z 
Csharp :: c# out parameter 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =