当前位置:网站首页>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边栏推荐
- Design and implementation of secsha system
- Object detection based on impulse neural network
- 二叉树递归套路总结
- Practice of concurrent search
- Multi camera stereo calibration
- 98. Verify the binary search tree ●●
- Initial experience | purchase and activate typora software
- 3D reconstruction of point cloud
- MySQL delete uniqueness constraint unique
- CIS benchmark tool Kube bench
猜你喜欢
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
21.PWM应用编程
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
2:第一章:认识JVM规范1:JVM简介;
orgchart. JS organization chart, presenting structural data in an elegant way
Week 17 homework
[classical control theory] summary of automatic control experiment
TVS管 与 稳压二极管参数对比
STM32__06—单通道ADC
《牛客刷verilog》Part III Verilog企业真题
随机推荐
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
Comparison of parameters between TVs tube and zener diode
Basic knowledge of database (interview)
Why use weak pointers for delegation- Why use weak pointer for delegation?
grafana工具界面显示报错influxDB Error
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
Introduction to JVM
Rethinking about MySQL query optimization
Code farmers to improve productivity
LeetCode——Add Binary
February 13, 2022-4-symmetric binary tree
11gR2 Database Services for &quot; Policy&quot; and &quot; Administrator&quot; Managed databases (file I
Difference between out of band and in band
When to use useImperativeHandle, useLayoutEffect, and useDebugValue
帶外和帶內的區別
C# Linq Demo
Go语言实现原理——Map实现原理
Xinyuan & Lichuang EDA training camp - brushless motor drive
AsyncSocket长连接棒包装问题解决
3D reconstruction of point cloud