【发布时间】:2019-10-15 02:47:36
【问题描述】:
我正在学习 c#,但偶然发现了一个问题。我试过在谷歌上环顾四周,将变量调用到另一个类上。我发现是通过使用 get set 方法。这是唯一的方法吗?
另一个问题是,我如何从列表中检索信息以打印到另一个类。
我做的代码可以在下面找到。
class Program
{
public int Xyz { get; set; } = 15;
public static void Main()
{
Console.WriteLine("Hello World");
List<int> Lst = new List<int>();
Lst.Add(1);
Lst.Add(2);
Lst.Add(3);
for (int x = 0; x < Lst.Count; x++)
{
Console.WriteLine("This is number {0}.", Lst[x]);
}
}
public class Tests
{
public void Next(int Xyz)
{
Console.WriteLine("Value of Xyz is: {0}", Xyz);
Console.WriteLine("{0}", Lst.Count);
List<int> Lst2 = new List<int>(Lst);
Console.WriteLine("Lst2 has {0} values.", Lst2.Count);
}
}
}
我想从中看到什么
Console.WriteLine("Value of Xyz is: {0}", Xyz);是值 15。
从
Console.WriteLine("{0}", Lst.Count); 值 3。
非常感谢!
【问题讨论】:
-
mjwills的方法还可以。我通过使用静态类 PublicZoom 提供了另一种方法。看我的回答。