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: |

The SUCK Channel on Telegram, with a message saying some content has been removed by the police. Photo: Telegram screenshot. More>> During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. Ng, who had pleaded not guilty to all charges, had been detained for more than 20 months. His channel was said to have contained around 120 messages and photos that incited others to vandalise pro-government shops and commit criminal damage targeting police stations. Other crimes that the SUCK Channel incited under Ng’s watch included using corrosive chemicals to make explosives and causing grievous bodily harm with intent. The court also found Ng responsible for calling on people to assist protesters who clashed violently with police at several universities in November 2019.
from us


Telegram C# (C Sharp) programming
FROM American