合格名单<类>作为参数,C#,使用“ref";关键词 [英] Passing List<Class> as argument, C#, using "ref" keyword
问题描述
朋友们!
所以我一直在写一个简单的程序.基本上,我有一个类(3 个字符串,1 个整数,一些方法).我正在从文本文件初始化此类的对象的值.我为此使用列表.问题是初始化是一个单独的函数.我在此函数中声明并初始化了 List BOTH.但是,我可能在其他函数中需要它,包括Program.Main".
So I have been writing a simple program. Basically, I have a class (3 strings, 1 int, some methods). I am initializing the values of objects of this class from a text file. I am using List for this. The problem is that initialization is a separate function. I had List BOTH declared AND initialized in this function. However, I might need it in other functions, including "Program.Main".
我应该创建一个全局"类并放置一个公共列表<类> ?我决定现在就在我的 Program.Main 函数中声明它.但是,我不确定 List 是按值传递还是按引用传递.我在网上找到了一个页面,建议使用 ref 关键字.现在我有:
Should I make a "Global" class and put make a public List< Class > ? I decided to just declare it in my Program.Main function as of now. However, I am not sure, if a List is passed by value or by reference. I found a page on the Web, which suggests using ref keyword. Now I have:
public Class FooClass
{...}
class Program
{
static void Main(string[] args)
{
List<FooClass> fooDB = new List<FooClass>;
initFromFile(ref fooDB);
}
private static initFromFile(ref List<FooClass> fooDB)
{
using (StreamReader ... )
{
while( ... )
{
...
fooDB.Add(new FooClass(args))
}
}
}//initFromFile
}//Program
我应该继续这样工作吗?或者有什么重要的建议?也许ref"根本不是一个好习惯?
Should I keep on working like this? Or are there any crucial suggestions? Maybe "ref" isn't a good practice at all?
TLDR:我应该制作一个全局 List 还是将其作为参考或其他方式传递(建议).如果通过引用传递,那么我应该使用 ref 关键字还是有其他方法?
TLDR: Should i make a global List or pass it as a reference or another way(suggest). If passing by reference, then should I use a ref keyword or is there another way?
提前致谢,
~~~hlrmn
推荐答案
你的程序结构很好,除了不需要使用ref
.List
是一个引用类型,意味着传递给函数的值实际上是一个引用(C中的指针"/C++ 术语)到 List
实例.因此,当函数返回时,对方法中列表的任何修改都将可用.
The way your program is structured is just fine, except there's no need toi use ref
. List<T>
is a reference type, meaning that the value that is passed to the function is actually a reference ("pointer" in C/C++ terms) to a List<T>
instance. So any modifications to the list within the method will be available when the function returns.
使用 ref
的唯一原因是,如果您想将 fooDB
指向一个新的 实例:
The only reason you would use ref
is if you wanted to point fooDB
to a new instance:
private static initFromFile(ref List<FooClass> fooDB)
{
fooDB = new List<FooClass>; // if the parameter was not passed by reference
// this instance would not be used by the caller.
using (StreamReader ... ){
while( ... )
{
...
fooDB.Add(new FooClass(args))
}}
}//initFromFile
编辑
仔细阅读你问题的第一部分,似乎 fooDB
应该是一个被初始化的 类成员:
Reading the first part of your question more closely, it seems like fooDB
should possibly be a class member that is initialized:
class Program
{
private static List<FooClass> fooDB;
static void Main(string[] args)
{
initFromFile();
foreach(FooClass f in fooDB)
{
// do something
}
}
private static initFromFile()
{
fooDB = new List<FooClass>();
using (StreamReader ... )
{
while( ... )
{
...
fooDB.Add(new FooClass(args))
}
}
}//initFromFile
}//Program
这篇关于合格名单<类>作为参数,C#,使用“ref";关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!