Access List from another class

后端 未结 3 1813
不知归路
不知归路 2020-12-09 06:16

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

3条回答
  •  时光说笑
    2020-12-09 06:46

    public class MyClass {
    
        private List myList = new List();
    
        public List GetList()
        {
            return myList;
        }
    } 
    

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

    public class CallingClass {
    
        MyClass myClass = new MyClass();
    
        public void GetList()
        {
            List calledList = myClass.GetList();
            ///More code here...
        }
    }
    

提交回复
热议问题