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

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 :: group by unique c# 
Csharp :: pyqt minimize to tray icon 
Csharp :: c# loop through repeater items 
Csharp :: json serialization 
Csharp :: parametrizedthreadstart C# 
Csharp :: c# remove all items from list where item value is null 
Csharp :: how to trim path in C# 
Csharp :: C# Read() and ReadKey() 
Csharp :: c# add key value pair to dictionary 
Csharp :: How to add rigidbody as a variable 
Csharp :: c# convert string to uri 
Csharp :: ihttpactionresult to object c# 
Csharp :: c# delete files 
Csharp :: mysql: [Warning] Using a password on the command line interface can be insecure. 
Csharp :: Scrollable WPF ListBox 
Csharp :: string in c# 
Csharp :: c# obsolete class 
Csharp :: c sharp 
Csharp :: c# list foreach lambda multiple actions 
Csharp :: count the number of notes in a given amount c# 
Csharp :: c# generic return type in interface 
Csharp :: find all factors of a given number 
Csharp :: C# Convert 1 range to another 
Csharp :: nunit cleanup after all tests 
Csharp :: linq from list c# 
Csharp :: how set format persian data picker to en 
Csharp :: c# calculate checksum of file 
Csharp :: unity rollaball 
Csharp :: web client ignore ssl error 
Csharp :: how to get length of okobjectresult c# 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =