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

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

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

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 core genetare class using existing database 
Csharp :: how to set border for groupbox in c# 
Csharp :: c# scroll to bottom of datagridview vb.net 
Csharp :: c# get logged on user name 
Csharp :: how to create function in c# 
Csharp :: unity2d movement 
Csharp :: linked list reverse 
Csharp :: print a file from C# 
Csharp :: c# form set auto scale 
Csharp :: encrypt with public key and decrypt with private key c# 
Csharp :: c# nullable generic 
Csharp :: switch statement c# example 
Csharp :: deploy .net core 
Csharp :: linq datatable 
Csharp :: c# get date without time 
Csharp :: c# export datatatble to excel 
Csharp :: c# get all letters 
Csharp :: return stream from file c# 
Csharp :: winforms input box 
Csharp :: update table in C# 
Csharp :: count the number of notes in a given amount c# 
Csharp :: how to mock http client c# 
Csharp :: Get unique id of Device 
Csharp :: context.Response.Body read stream .net 
Csharp :: c# Intersectcase insensitive 
Csharp :: c# custom event handler with parameters 
Csharp :: Send Hotmail/Outlook Email C# (Win/ASP.NET) 
Csharp :: c# Case insensitive Contains(string) 
Csharp :: how to parse mongo db json in c# 
Csharp :: c# dictionary check if value exists 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =