Reputation: 4079
friends!
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".
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
Should I keep on working like this? Or are there any crucial suggestions? Maybe "ref" isn't a good practice at all?
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?
Thanks in advance,
~~~hlfrmn
Upvotes: 0
Views: 15516
Reputation: 152556
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.
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
EDIT
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
Upvotes: 8
Reputation: 1
Given the code sample above, I would suggest that your initFromFile() method just return the list, i.e.
public Class FooClass
{...}
class Program
{static void Main(string[] args)
{
var fooData = initFromFile();
}
private static List<FooClass> initFromFile()
{
var fooDB = new List<FooClass>();
using (StreamReader ... ){
while( ... )
{
...
fooDB.Add(new FooClass(args))
}}
return fooDB;
}//initFromFile
}//Program
Upvotes: 0
Reputation: 149020
Unless you're trying to set the value of fooDB
directly inside the initFromFile
function, you don't need to pass it as a ref
parameter. Note that classes in .NET are reference types, so when you pass the list to method, the method parameter refers to the same instance that was passed in (as opposed to value types). ref
parameters are used to modify what value that the both the parameter and the variable that was passed into the method reference. Using ref
parameters is almost never necessary in typical .NET development.
Just pass it normally, and it will work:
static void Main(string[] args)
{
List<FooClass> fooDB = new List<FooClass>;
initFromFile(fooDB);
}
private static initFromFile(List<FooClass> fooDB)
{
using (StreamReader ... )
{
while( ... )
{
...
fooDB.Add(new FooClass(args))
}
}
}
Upvotes: 0
Reputation: 101681
Generic lists are reference types
hence you don't need to use ref
keyword.If you make a change in your list on your method, it will affect your actual list.
Upvotes: 0