dlang ref的作用

ref 作用

复制代码
 1 import std.stdio, std.string;
 2 
 3 void main()
 4 {
 5     string[] color=["red","blue","cyan","grey"];
 6     
 7     writeln("raw:",color);
 8     write("m1:");
 9     m1(color);
10     write("m2:");
11     m2(color);
12 }
13 
14 void m1(string[] color)
15 {
16     foreach(string col; color)
17     {
18         if(col=="red") // 此处col是color中每个元素的copy,直接赋值无法改变原来数组
19         {
20             col="tomato"; 
21         }
22     }
23     writeln(color);
24 }
25 
26 void m2(string[] color)
27 {
28     foreach(ref string col; color) //添加ref修饰,可直接作用到原数组元素
29     {
30         if(col=="red") 
31         {
32             col="tomato";
33         }
34     }
35     writeln(color);
36 }
复制代码

 

posted @   天使不设防  阅读(133)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示