如何将值添加到另一个类的列表中

编程入门 行业动态 更新时间:2024-10-22 03:06:29
本文介绍了如何将值添加到另一个类的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

public class Record { public List<Fie> Field = new List<Fie>();

}

}

public class Fie { public string Byt { get; set; } public string Mon(string a) { string b=a; return a; } }

我我在函数Mon中得到一个字符串。然后我必须将该值发送到Public string Byt。之后,我必须在列表字段中添加Byt。我不知道该怎么做。请发布答案。在此先感谢。

I am getting a string in function Mon. Then I have to send that value to Public string Byt. After that I have to add the Byt in the list Field. I dont know how to do. Please post the answer. Thanks in Advance.

推荐答案

从这里开始:演练:创建自己的集合类 [ ^ ]

你不能把它从Fie类中添加到列表中 - 它没有' t(并且绝对不应该)知道Fie实例列表的存在,因此无法以任何方式访问它。 添加新的你的列表中的Fie的例子很简单: You can't add it to the list from within the Fie class - it doesn't (and definitely shouldn't) know even of the existence of the List of Fie instances so it can't access it in any way. To add a new instance of Fie to your list is simple: public class Record { public List<Fie> Field = new List<Fie>(); void AddIt(string s) { Field.Add(new Fie(s)); } }

但直接公开公开你的字段也被认为是不好的做法 - 你应该使用一个属性(并且可能返回一个列表的副本而不是实际的列表本身。)

But it is also considered poor practice to publicly expose your fields directly - you should use a property instead (and possibly return a copy of the list instead of the actual list itself).

更多推荐

如何将值添加到另一个类的列表中

本文发布于:2023-11-23 15:55:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!