当前位置:网站首页>In C#, why can't I modify the member of a value type instance in a foreach loop?
In C#, why can't I modify the member of a value type instance in a foreach loop?
2022-07-05 23:30:00 【Superior virtue and weak water】
problem :
I know that value types should be immutable, but that's just a suggestion, not a rule, right? I know that the value type should be immutable , But this is just a suggestion , Not the rules , Right ?So why can't I do something like this: Then why can't I do such a thing :
struct MyStruct{ public string Name { get; set; }} public class Program{ static void Main(string[] args) { MyStruct[] array = new MyStruct[] { new MyStruct { Name = "1" }, new MyStruct { Name = "2" } }; foreach (var item in array) { item.Name = "3"; } //for (int i = 0; i < array.Length; i++) //{ // array[i].Name = "3"; //} Console.ReadLine(); }}The foreach loop in the code doesn't compile while the commented for loop works fine. In code foreach Loop cannot compile , And annotated for The cycle works normally .The error message: error message :
Cannot modify members of 'item' because it is a 'foreach iteration variable' Can't modify “item” Members of , Because it is “foreach Iterative variable ”
Why is that? Why is that ?
Solution :
Reference resources : https://stackoom.com/en/question/Ni0P边栏推荐
- How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
- 派对的最大快乐值
- 两数之和、三数之和(排序+双指针)
- 2022.6.20-6.26 AI行业周刊(第103期):新的小生命
- Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
- Selenium+pytest automated test framework practice
- The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous
- Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
- From the perspective of quantitative genetics, why do you get the bride price when you get married
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
猜你喜欢

数学公式截图识别神器Mathpix无限使用教程

21.PWM应用编程

2:第一章:认识JVM规范1:JVM简介;

Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang

Neural structured learning - Part 2: training with natural graphs

3:第一章:认识JVM规范2:JVM规范,简介;
![[classical control theory] summary of automatic control experiment](/img/22/9c9e107da7e305ce0a57d55b4d0b5a.png)
[classical control theory] summary of automatic control experiment

Huawei simulator ENSP - hcip - MPLS experiment

Dynamic memory management (malloc/calloc/realloc)

Practice of concurrent search
随机推荐
Sum of two numbers, sum of three numbers (sort + double pointer)
3D point cloud slam
Creative mode 1 - single case mode
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
2022.6.20-6.26 AI行业周刊(第103期):新的小生命
Go language implementation principle -- map implementation principle
Introduction to JVM
秒杀系统的设计与实现思路
开关电源Buck电路CCM及DCM工作模式
Leecode learning notes
11gR2 Database Services for &quot; Policy&quot; and &quot; Administrator&quot; Managed databases (file I
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
【经典控制理论】自控实验总结
Using LNMP to build WordPress sites
Huawei simulator ENSP - hcip - MPLS experiment
How to design API return code (error code)?
When to use useImperativeHandle, useLayoutEffect, and useDebugValue
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
Neural structured learning - Part 3: training with synthesized graphs
Multi camera stereo calibration