c# List find()方法返回值的问题说明(返回结果为对象的指针)
时间:2023-11-20 21:55:34
C#中List<T>中泛型T如果是一个对象的话,则利用Find函数返回的将是这个对象的指针,对其返回对象的属性进行操作,也会影响list中相应元素对象的值。验证如下:
1.新建一个Class1类,其含有两个姓名和分数两个属性:
<SPAN style="FONT-SIZE: 18px"> public class Class1
{
public string name{ get; set; }
public int score { get; set; }
}</SPAN>
2.在窗口类中写入如下代码,在button1_Click的事件中的右括号处设置断点。
<SPAN style="FONT-SIZE: 18px"> public Form1()
{
InitializeComponent();
}
Class1 a = new Class1();
Class1 b = new Class1();
List<Class1> l = new List<Class1>();
private void button1_Click(object sender, EventArgs e)
{
string name = "小明";
Class1 c = l.Find(delegate(Class1 p) { return (p.name.Equals(name)); });
c.score = 100;
}
private void Form1_Load(object sender, EventArgs e)
{
a.name = "小明";
a.score = 90;
l.Add(a);
b.name = "小红";
b.score = 99;
l.Add(b);
}</SPAN>
3.运行结果如下:
由图可知,想button事件中对对象c的改变,使list中相应对象也发生了改变。故其返回值为对象的引用
标签:List,find,返回值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis-plus实现主键自增和自动注入时间的示例代码
2022-11-05 00:22:51
c#如何实现接口事件
2023-10-02 18:31:28
Java实例讲解注解的应用
2021-11-02 01:43:14
让Java后台MySQL数据库能够支持emoji表情的方法
2022-12-30 04:24:45
Java8 Collectors.toMap的坑
2023-06-24 14:47:48
浅谈Spring框架中@Autowired和@Resource的区别
2023-11-26 11:31:21
![](https://img.aspxhome.com/file/2023/6/96096_0s.png)
SpringBoot中配置SSL的同时支持http和https访问
2022-06-30 00:58:58
![](https://img.aspxhome.com/file/2023/3/106153_0s.jpg)
FeignClient实现接口调用方式(不同参数形式)
2023-03-06 08:41:39
![](https://img.aspxhome.com/file/2023/4/69094_0s.png)
Java中使用StackWalker和Stream API进行堆栈遍历
2023-04-12 11:29:07
Android实现拍照或者选取本地图片
2022-06-16 03:58:31
![](https://img.aspxhome.com/file/2023/6/120066_0s.jpg)
spring webflux自定义netty 参数解析
2023-07-26 18:38:25
![](https://img.aspxhome.com/file/2023/1/57811_0s.jpg)
一文带你了解Android中的网络请求
2023-08-11 23:46:38
![](https://img.aspxhome.com/file/2023/4/138134_0s.png)
C# 特性AttributeUsage简介与使用教程
2022-09-30 01:53:21
![](https://img.aspxhome.com/file/2023/1/111141_0s.png)
自定义时间格式转换代码分享
2022-11-03 03:09:23
Spring4整合Hibernate5详细步骤
2022-03-15 16:53:07
Android WebView实现网页滚动截图
2022-12-12 12:13:03
![](https://img.aspxhome.com/file/2023/8/88098_0s.jpg)
基于JavaMail API收发邮件的方法
2022-03-10 09:34:24
剑指Offer之Java算法习题精讲链表与二叉树专项训练
2022-01-12 19:19:01
![](https://img.aspxhome.com/file/2023/9/129189_0s.png)
spring web.xml指定配置文件过程解析
2023-05-15 01:32:40
SpringMVC+Mybatis实现的Mysql分页数据查询的示例
2023-11-24 20:53:33
![](https://img.aspxhome.com/file/2023/2/58692_0s.png)