CSHARP_CI Telegram 1425
Задача: Замыкания в циклах и порядок выполнения

Рассмотрим следующий код на C#:


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program
{
static async Task Main()
{
var tasks = new List<Task>();

for (int i = 0; i < 5; i++)
{
tasks.Add(Task.Run(() => Console.WriteLine($"Значение i = {i}")));
}

await Task.WhenAll(tasks);
}
}


Вопросы:

Что выведет эта программа при запуске и почему?

Как минимально изменить код (не затрагивая содержимое Task.Run), чтобы на консоль выводились числа от 0 до 4 (по одному разу каждое)?

Подсказка: лямбда внутри Task.Run замыкает переменную i, а не её текущее значение.

@csharp_ci



tgoop.com/csharp_ci/1425
Create:
Last Update:

Задача: Замыкания в циклах и порядок выполнения

Рассмотрим следующий код на C#:


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program
{
static async Task Main()
{
var tasks = new List<Task>();

for (int i = 0; i < 5; i++)
{
tasks.Add(Task.Run(() => Console.WriteLine($"Значение i = {i}")));
}

await Task.WhenAll(tasks);
}
}


Вопросы:

Что выведет эта программа при запуске и почему?

Как минимально изменить код (не затрагивая содержимое Task.Run), чтобы на консоль выводились числа от 0 до 4 (по одному разу каждое)?

Подсказка: лямбда внутри Task.Run замыкает переменную i, а не её текущее значение.

@csharp_ci

BY C# (C Sharp) programming


Share with your friend now:
tgoop.com/csharp_ci/1425

View MORE
Open in Telegram


Telegram News

Date: |

While the character limit is 255, try to fit into 200 characters. This way, users will be able to take in your text fast and efficiently. Reveal the essence of your channel and provide contact information. For example, you can add a bot name, link to your pricing plans, etc. With Bitcoin down 30% in the past week, some crypto traders have taken to Telegram to “voice” their feelings. How to create a business channel on Telegram? (Tutorial) It’s yet another bloodbath on Satoshi Street. As of press time, Bitcoin (BTC) and the broader cryptocurrency market have corrected another 10 percent amid a massive sell-off. Ethereum (EHT) is down a staggering 15 percent moving close to $1,000, down more than 42 percent on the weekly chart. Matt Hussey, editorial director of NEAR Protocol (and former editor-in-chief of Decrypt) responded to the news of the Telegram group with “#meIRL.”
from us


Telegram C# (C Sharp) programming
FROM American