如何在C#中的另一个类中调用列表值? [英] How to call the list value in another class in C#?

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

问题描述

你好。我很容易,但我有一个不知道的问题。首先,我很抱歉这么容易问你。在我的手中。我有一个列表作为列表。如果列表保持数字,则该值小于10。我想在另一个类中打印该值。我该怎么打印呢。





Numbers.cs



Hello.I'm easy but I have a question for not knowing.First of all, I'm sorry to ask you so easily.cs. in my hand.I have a list as a list.This value is less than 10 in the case of numbers as the list holds.I want to print this value in another class. How can I print it.


Numbers.cs

public List<string> numberlist=new List<string>







CalculateProcess.cs



//在这个课程中,我想打印此列表中的值。如何打印。



我尝试过:



Numbers.cs




CalculateProcess.cs

//In this class I want to print the value in this list.How do I print.

What I have tried:

Numbers.cs

public List<string> numberlist=new List<string>





CalculateProcess.cs



//试试代码。



CalculateProcess.cs

// try code.

Console.Writeline(numberlist);  or  NumberFiles.Send(path,number,numberlist,"success");

推荐答案

检查我的过去答案:如何在另一个班级中调用列表? [ ^ ]
Check my past answer: How do I call list in another class?[^]


这不是很复杂,但看起来似乎如此。你需要的是使用数字类的实例来访问列表。

这可能并不意味着什么,但这是你每天都做的事情,甚至没有考虑过。



所以......让我们忽略它,想想一辆车。假设您将手机放在汽车的手套箱中 - 然后将其换成新的。如果你打开手套箱,你期望你的手机在那里吗?当然不是!它们都是你的车,但车辆的实际情况已发生变化。而且你知道:你的车不同于我的车,他的车,她的车。一辆车可以通过几个名字访问:你的车,这辆车,那辆车 - 所有车辆都指同一辆车。

为了开车去商店,你需要拥有正确的汽车实例:你的车而不是我的车。



和类是一样的:为了使用类的元素,你需要指定你正在讨论的类的哪个实例。在类定义中很简单:你有这个(明确地或隐含地),它表示当前实例,无论它是哪一个。

但是在外面这个类更复杂:你必须找到实例,并参考它:

It's not really complicated, but it can seem so. What you need is to use the instance of the numbers class to access the list.
And that probably doesn't mean anything, but it's something you do every day and don't even think about.

So ... lets ignore it, and think about a car. Suppose you put your mobile in the glove box of your car - and then trade it in on a new one. If you open the glove box, do you expect your mobile to be there? Of course not! They are both "your car", but the actual instance of a vehicle has changed. And you know that: "your car" is different from "my car", "his car", "her car". And a single car may be accessed by several names: "your car", "this car", "that car" - the all refer to the same vehicle.
In order to drive to the shops, you need to have the right instance of a car: "your car" rather than "my car".

And classes are the same: in order to use elements of a class you need to specify which instance of the class you are talking about. Inside the class definition that's easy: you have this (Explicitly or implicitly) which says "the current instance" whichever one it is.
But outside the class it's more complicated: you have to find the instance, and refer to it:
Car myCar = new Car("Mercedes", "A-class", Color.Black);
...
myCar.Engine.Start();</pre

<pre lang="c#">Car yourCar = new Car("Ford", "Fiesta", Color.Red);
...
yourCar.GLoveBox.Add(yourMobile);





所以要访问你的清单,你只需要提供实例(通过变量)并指定集合。



另一种方法是使列表 static ,并通过类名访问它 - 但是在这种情况下,该类的所有实例只有一个列表(如果你愿意,静态手套箱里面会有一个虫洞,意味着同样的手套箱会从所有汽车访问 - 在现实世界中不实用,可能不是你想要的应用程序!)



So to access your list, you just need to supply the instance (via a variable) and specify the collection.

The other way is to make the list static, and access it via the class name - but in that case there is only one list for all instances of the class (if you like, a static glove box would have a wormhole in it which meant the same glove box would be accessed from all cars - not practical in the real world, and probably not what you want in your app!)


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

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