简体   繁体   English

如何在不创建新数组的情况下引用数组?

[英]How to refer to an array without creating a new one?

I want to refer to my USERS list in a form without having to create a new instance of the class the list is stored in as it creates a new list. 我想以一种形式引用我的用户列表,而不必在创建新列表时创建该列表所存储的类的新实例。

The LoginHandler class where the list is created and is added to. 创建列表并将其添加到的LoginHandler类。

namespace BG
{
    public class LoginHandler
    {
         public List<User> users = new List<User>();

         public LoginHandler()
         {
            users = new List<User>();
         }

The form will consist of a for loop which loops to the count of items in the list so i need to refer back to it but do not want to have to create a new instance of class its in because that makes a new list which i do not want. 该表单将包含一个for循环,该循环循环到列表中的项目数,因此我需要引用它,但又不想创建一个新的class实例,因为它会创建一个新的列表,不想。

I think what you want is a property that returns a reference to a IEnumerable object. 我认为您想要的是一个返回对IEnumerable对象的引用的属性。

public class LoginHandler
{
    private List<User> users = new List<User>();
    public IEnumerable<User> Users { get { return users; } }
}

Then you can access the property without creating a copy or exposing the internal data members. 然后,您可以访问属性而无需创建副本或公开内部数据成员。

LoginHandler handler = new LoginHandler();
foreach (var user in handler.Users)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不造成混乱的情况下引用多个类? - How to refer to multiple classes without creating a mess? c#) 如何在不复制新数组的情况下引用 1mb 字节数组的内容 - c#) How to refer to the contents of 1mb byte array, without copy new array 如何在不创建新列表的情况下重命名列表中的值? - How to rename value in a List without creating a new one? 如何在不创建新数组的情况下用字符串填充字节数组? - How can i fill a byte array with a string without creating a new array? 为什么执行新线程而不创建新线程? - Why a new thread executed without creating a new one? 如何将值从一种形式传递到另一种形式,而无需在C#中创建形式的新实例? - how to pass a values from one form to another form without creating new instance of a form in c#? 如何在不创建新集合的情况下创建新分区? - How to create new Partitions without creating a new collection? 从排序数组中删除重复项而不创建新数组 - Removing duplicates from a sorted array without making a new one 如何在不创建新控件的情况下重写ListView Items属性? - how to override ListView Items Property without creating new control? 如何在不创建新表单实例的情况下在表单之间切换? - How to switch between forms without creating new instance of forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM