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

At a time when the Indian stock market is peaking and has rallied immensely compared to global markets, there are companies that have not performed in the last 10 years. These are definitely a minor portion of the market considering there are hundreds of stocks that have turned multibagger since 2020. What went wrong with these stocks? Reasons vary from corporate governance, sectoral weakness, company specific and so on. But the more important question is, are these stocks worth buying?

Telegram auto-delete message, expiring invites, and more

elegram is updating its messaging app with options for auto-deleting messages, expiring invite links, and new unlimited groups, the company shared in a blog post. Much like Signal, Telegram received a burst of new users in the confusion over WhatsApp’s privacy policy and now the company is adopting features that were already part of its competitors’ apps, features which offer more security and privacy. Auto-deleting messages were already possible in Telegram’s encrypted Secret Chats, but this new update for iOS and Android adds the option to make messages disappear in any kind of chat. Auto-delete can be enabled inside of chats, and set to delete either 24 hours or seven days after messages are sent. Auto-delete won’t remove every message though; if a message was sent before the feature was turned on, it’ll stick around. Telegram’s competitors have had similar features: WhatsApp introduced a feature in 2020 and Signal has had disappearing messages since at least 2016.

C 1001 notes from tw


Telegram C# 1001 notes
FROM USA