C# struct:无法修改“...”的返回值,因为它不是变量原因

本文解析了值类型默认按值传递的原因,及其为何无法直接修改原始值的问题。提供了两种解决方案:一是转换为引用类型,二是使用中间变量替换整个结构体。

出现这个原因是值类型默认是按值传递的,返回的是栈上的一份临时的、本地的拷贝的值

因此即使能对它进行修改也无法反映到原来的值本身上,这是一个毫无何意义的操作,因此编译器就从源头上禁止了这样的操作。

解决办法:

1.改成class引用类型,这样,就成了一个对象(引用类型),就会返回在堆上的实际地址,修改也会反映到实际的对象 

2.设置一个中间临时变量,然后再替换整个原来的struct值。这样实际上是替换整个struct。

模块1 Rebuild started: Project: 仓库粉尘检测与安全控制系统的设计A Rebuild target 'Target 1' assembling STARTUP.A51... compiling delay.c... compiling ds1302.c... ..\sources\ds1302.c(7): error C202: 'DS1302_RST': undefined identifier ..\sources\ds1302.c(8): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(15): error C202: 'DS1302_RST': undefined identifier ..\sources\ds1302.c(20): error C202: 'DS1302_IO': undefined identifier ..\sources\ds1302.c(22): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(24): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(30): error C202: 'DS1302_IO': undefined identifier ..\sources\ds1302.c(32): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(34): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(38): error C202: 'DS1302_RST': undefined identifier ..\sources\ds1302.c(45): error C202: 'DS1302_RST': undefined identifier ..\sources\ds1302.c(51): error C202: 'DS1302_IO': undefined identifier ..\sources\ds1302.c(53): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(55): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(60): error C202: 'DS1302_IO': undefined identifier ..\sources\ds1302.c(63): error C202: 'DS1302_IO': undefined identifier ..\sources\ds1302.c(64): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(66): error C202: 'DS1302_CLK': undefined identifier ..\sources\ds1302.c(70): error C202: 'DS1302_RST': undefined identifier compiling dust_sensor.c... compiling oled12864.c... compiling mainA.C... ..\sources\mainA.C(53): warning C206: 'OLED_ShowString': missing function-prototype ..\sources\mainA.C(53): error C267: 'OLED_ShowString': requires ANSI-style prototype ..\sources\mainA.C(83): error C141: syntax error near 'static', expected '__asm' ..\sources\mainA.C(83): error C202: 'init_time': undefined identifier ..\sources\mainA.C(95): error C202: 'init_time': undefined identifier Target not created. Build Time Elapsed: 00:00:01
06-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值