当前位置:网站首页>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边栏推荐
- 2022.6.20-6.26 AI行业周刊(第103期):新的小生命
- 424. 替换后的最长重复字符 ●●
- Multi view 3D reconstruction
- Xinyuan & Lichuang EDA training camp - brushless motor drive
- Neural structured learning - Part 2: training with natural graphs
- AsyncSocket长连接棒包装问题解决
- Introduction to JVM
- 424. The longest repeated character after replacement ●●
- Detailed explanation of pointer and array written test of C language
- UVA – 11637 Garbage Remembering Exam (组合+可能性)
猜你喜欢
【LeetCode】5. Valid Palindrome·有效回文
3:第一章:认识JVM规范2:JVM规范,简介;
《牛客刷verilog》Part III Verilog企业真题
MySQL delete uniqueness constraint unique
Realize reverse proxy client IP transparent transmission
无刷驱动设计——浅谈MOS驱动电路
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
Go语言实现原理——Map实现原理
动态规划 之 打家劫舍
Neural structured learning 4 antagonistic learning for image classification
随机推荐
698. Divided into k equal subsets ●●
【原创】程序员团队管理的核心是什么?
poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
C# Linq Demo
C Primer Plus Chapter 9 question 9 POW function
The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
Dynamic planning: robbing families and houses
[classical control theory] summary of automatic control experiment
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
Leetcode buys and sells stocks
Three. JS VR house viewing
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
Three. Js-01 getting started
20.移植Freetype字体库
Scala concurrent programming (II) akka
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
Golang code checking tool
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
2.13 summary