如何在另一个班级中呼叫列表? [英] How do I call list in another class?

查看:75
本文介绍了如何在另一个班级中呼叫列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我在两个班级之间遇到这个问题。在第1类中,有一个值作为列表。我想用console.writeline在第二类ta中打印此列表。在这种情况下,如何在另一个类和打印列表中调用列表值。 />


Definition.cs

Hello.I'm having this problem between two classes. In the 1st class, there is a value as a list.I would like to print this list in the 2nd class ta with console.writeline.In this case, how can I call the list value in another class and print list.

Definition.cs

public List<string> ListValue=new List<string> ();



Usebility.cs(通话类)



我尝试过:




Usebility.cs (call class)

What I have tried:

Console.WriteLine( ListValue);

推荐答案

假设 ListValue 是可访问的......

你需要遍历<$ c $中的项目集合c>列表< string> ,例如:

Assuming that ListValue is accesible...
You need to loop through the collection of items in a List<string>, for example:
foreach(string s in ListValue)
    Console.WriteLine(s);


这是一种方式:

Here is one way:
ing System;
using System.Collections.Generic;

namespace classSample
{
    class Program
    {
        static void Main(string[] args)
        {
            ClassTwo cTwo = new ClassTwo();
            List<string> theList = cTwo.GotThem;
            foreach (string item in theList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
    }
    public class ClassOne
    {
        public List<string> GetMe = new List<string>();

        public ClassOne()
        {
            GetMe.Add("one");
            GetMe.Add("two");
            GetMe.Add("three");
        }

    }
    public class ClassTwo
    {
        public List<string> GotThem { get; set; }

        public ClassTwo()
        {
            ClassOne cOne = new ClassOne();
            GotThem = cOne.GetMe;
        }
    }
}


这篇关于如何在另一个班级中呼叫列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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