Telegram Group & Telegram Channel
🧠 C# Π—Π°Π΄Π°Ρ‡Π°: β€œΠ›ΠΎΠ²ΡƒΡˆΠΊΠ° замыкания Π² циклС”

πŸ“œ УсловиС:

ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈ Π½Π° этот ΠΊΠΎΠ΄ ΠΈ скаТи, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚:


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 5; i++)
{
actions.Add(() => Console.Write(i + " "));
}

foreach (var action in actions)
{
action();
}
}
}


❓ Вопросы:
1. Π§Ρ‚ΠΎ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°?
2. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΠ΄ΠΈΠ²ΠΈΡ‚ΡŒ?
3. Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠ΄, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 0 1 2 3 4?

⚠️ ΠŸΠΎΠ΄Π²ΠΎΡ…:

- ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ i захватываСтся ΠΏΠΎ ссылкС всСми лямбдами Π² List<Action>, Π° Π½Π΅ копируСтся Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ добавлСния.
- Когда Ρ†ΠΈΠΊΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ, i == 5, ΠΈ всС лямбды ΠΎΠ±Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΈ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ `i`, ΡƒΠΆΠ΅ Ρ€Π°Π²Π½ΠΎΠΌΡƒ 5.

---

### βœ… ΠžΠΆΠΈΠ΄Π°Π΅ΠΌΡ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄:


5 5 5 5 5


πŸ› οΈ Π˜ΡΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅:

Π§Ρ‚ΠΎΠ±Ρ‹ каТдая лямбда Π·Π°Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Π»Π° свою копию `i`, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ:


for (int i = 0; i < 5; i++)
{
int copy = i;
actions.Add(() => Console.Write(copy + " "));
}


Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚:


0 1 2 3 4


🎯 Π§Ρ‚ΠΎ провСряСт Π·Π°Π΄Π°Ρ‡Π°:

- Π—Π½Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ…Π°Π½ΠΈΠΊΠΈ Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΉ Π² C#
- ПониманиС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ значСниями ΠΈ ссылками Π² Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΈ
- Π£ΠΌΠ΅Π½ΠΈΠ΅ ΠΎΡ‚Π»Π°ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² LINQ ΠΈ Π΄Π΅Π»Π΅Π³Π°Ρ‚Π°Ρ…



tg-me.com/csharp_1001_notes/687
Create:
Last Update:

🧠 C# Π—Π°Π΄Π°Ρ‡Π°: β€œΠ›ΠΎΠ²ΡƒΡˆΠΊΠ° замыкания Π² циклС”

πŸ“œ УсловиС:

ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈ Π½Π° этот ΠΊΠΎΠ΄ ΠΈ скаТи, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚:


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 5; i++)
{
actions.Add(() => Console.Write(i + " "));
}

foreach (var action in actions)
{
action();
}
}
}


❓ Вопросы:
1. Π§Ρ‚ΠΎ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°?
2. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΠ΄ΠΈΠ²ΠΈΡ‚ΡŒ?
3. Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠ΄, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 0 1 2 3 4?

⚠️ ΠŸΠΎΠ΄Π²ΠΎΡ…:

- ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ i захватываСтся ΠΏΠΎ ссылкС всСми лямбдами Π² List<Action>, Π° Π½Π΅ копируСтся Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ добавлСния.
- Когда Ρ†ΠΈΠΊΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ, i == 5, ΠΈ всС лямбды ΠΎΠ±Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΈ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ `i`, ΡƒΠΆΠ΅ Ρ€Π°Π²Π½ΠΎΠΌΡƒ 5.

---

### βœ… ΠžΠΆΠΈΠ΄Π°Π΅ΠΌΡ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄:


5 5 5 5 5


πŸ› οΈ Π˜ΡΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅:

Π§Ρ‚ΠΎΠ±Ρ‹ каТдая лямбда Π·Π°Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Π»Π° свою копию `i`, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ:


for (int i = 0; i < 5; i++)
{
int copy = i;
actions.Add(() => Console.Write(copy + " "));
}


Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚:


0 1 2 3 4


🎯 Π§Ρ‚ΠΎ провСряСт Π·Π°Π΄Π°Ρ‡Π°:

- Π—Π½Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ…Π°Π½ΠΈΠΊΠΈ Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΉ Π² C#
- ПониманиС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ значСниями ΠΈ ссылками Π² Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΈ
- Π£ΠΌΠ΅Π½ΠΈΠ΅ ΠΎΡ‚Π»Π°ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² LINQ ΠΈ Π΄Π΅Π»Π΅Π³Π°Ρ‚Π°Ρ…

BY C# 1001 notes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/csharp_1001_notes/687

View MORE
Open in Telegram


C 1001 notes Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Mining Work?

Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. β€œThis is how new coins are created” and new transactions are added to the blockchain, says Okoro.

Pinterest (PINS) Stock Sinks As Market Gains

Pinterest (PINS) closed at $71.75 in the latest trading session, marking a -0.18% move from the prior day. This change lagged the S&P 500's daily gain of 0.1%. Meanwhile, the Dow gained 0.9%, and the Nasdaq, a tech-heavy index, lost 0.59%. Heading into today, shares of the digital pinboard and shopping tool company had lost 17.41% over the past month, lagging the Computer and Technology sector's loss of 5.38% and the S&P 500's gain of 0.71% in that time. Investors will be hoping for strength from PINS as it approaches its next earnings release. The company is expected to report EPS of $0.07, up 170% from the prior-year quarter. Our most recent consensus estimate is calling for quarterly revenue of $467.87 million, up 72.05% from the year-ago period.

C 1001 notes from kr


Telegram C# 1001 notes
FROM USA