来自另一个类的访问列表 [英] Access List from another class

查看:95
本文介绍了来自另一个类的访问列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以有人告诉我如何在一个类中创建一个列表并从另一个类中访问?

can anyone tell me how to create a list in one class and access it from another?

推荐答案

public class MyClass {

    private List<string> myList = new List<string>();

    public List<string> GetList()
    {
        return myList;
    }
} 

你可以有任何东西而不是字符串。
现在,您可以创建一个MyClass的对象,并且可以访问您实现的返回myList的公共方法。

You can have any anything there instead of string. Now you can make an object of MyClass and can access the pubic method where you have implemented to return myList.

public class CallingClass {

    MyClass myClass = new MyClass();

    public void GetList()
    {
        List<string> calledList = myClass.GetList();
        ///More code here...
    }
}

这篇关于来自另一个类的访问列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆