当前位置:网站首页>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边栏推荐
- Idea connects to MySQL, and it is convenient to paste the URL of the configuration file directly
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
- Calculating the number of daffodils in C language
- golang代码检查工具
- What is the process of building a website
- Golang code checking tool
- Objective C message dispatch mechanism
- YML configuration, binding and injection, verification, unit of bean
- 3D point cloud slam
- Initial experience | purchase and activate typora software
猜你喜欢

Selenium+pytest automated test framework practice

Huawei simulator ENSP - hcip - MPLS experiment

How to design API return code (error code)?

CIS benchmark tool Kube bench

Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic

并查集实践

Sum of two numbers, sum of three numbers (sort + double pointer)

MySQL delete uniqueness constraint unique

Object detection based on impulse neural network
![Development specification: interface unified return value format [resend]](/img/3e/8751b818147cabbe22e4ce44af7d24.jpg)
Development specification: interface unified return value format [resend]
随机推荐
Judge whether the binary tree is a complete binary tree
2:第一章:认识JVM规范1:JVM简介;
进击的技术er——自动化
From the perspective of quantitative genetics, why do you get the bride price when you get married
3D point cloud slam
Naoqi robot summary 26
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
698. 划分为k个相等的子集 ●●
C Primer Plus Chapter 9 question 9 POW function
6-axis and 9-axis IMU attitude estimation
代码农民提高生产力
White hat talks about web security after reading 2
regular expression
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous
Idea connects to MySQL, and it is convenient to paste the URL of the configuration file directly
UVA – 11637 garbage remembering exam (combination + possibility)
MySQL replace primary key delete primary key add primary key
LeetCode——Add Binary