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

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# read double 
Csharp :: c# foreach namevaluecollection 
Csharp :: unity respawn c# 
Csharp :: get number of days between two dates c# 
Csharp :: c# count directories in directory and subdirectories 
Csharp :: how read excel data in c# 
Csharp :: unity switch to scene and transfer data 
Csharp :: get enum value c# 
Csharp :: destroy gameobject with tag unity 
Csharp :: check file lock c# 
Csharp :: c# listview add item 
Csharp :: c# convert double to string 
Csharp :: razor concatonate inline 
Csharp :: window height in C# forms 
Csharp :: unity agent look at 
Csharp :: color32 unity 
Csharp :: c# round to closest multiple 
Csharp :: unity unit tests 
Csharp :: c# destroy function...unity 
Csharp :: convert path to uri c# 
Csharp :: audio unity 
Csharp :: c# xmldocument from file 
Csharp :: c# standard microphone decibels 
Csharp :: asp.net get most recent file in directory 
Csharp :: wpf listboxitem event command 
Csharp :: what is float in c# 
Csharp :: flat view player movement script 
Csharp :: c sharp system pause equivelent 
Csharp :: access label from another class c# 
Csharp :: bind repeater to dictionary 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =