当前位置:网站首页>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边栏推荐
- 11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 I
- Attacking technology Er - Automation
- (4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
- Use of metadata in golang grpc
- Go language implementation principle -- map implementation principle
- Creative mode 1 - single case mode
- Multi view 3D reconstruction
- Rethinking about MySQL query optimization
- Multi camera stereo calibration
- Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
猜你喜欢
![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)
[original] what is the core of programmer team management?

CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)

TVS管和ESD管的技術指標和選型指南-嘉立創推薦

Realize reverse proxy client IP transparent transmission

Comparison of parameters between TVs tube and zener diode

Using LNMP to build WordPress sites

Pyqt control part (I)

Huawei simulator ENSP - hcip - MPLS experiment

Initial experience | purchase and activate typora software

数据库基础知识(面试)
随机推荐
判断二叉树是否为完全二叉树
Latex multiple linebreaks
《牛客刷verilog》Part III Verilog企业真题
进击的技术er——自动化
Switching power supply buck circuit CCM and DCM working mode
STM32__06—单通道ADC
Leetcode sword finger offer brush questions - day 21
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
98. 验证二叉搜索树 ●●
Go语言实现原理——Map实现原理
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
Go language implementation principle -- lock implementation principle
asp. Net pop-up layer instance
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
CIS基准测试工具kube-bench使用
动态规划 之 打家劫舍
LeetCode——Add Binary
Leetcode buys and sells stocks
Development specification: interface unified return value format [resend]