O
Ojas
I'm wondering if there is a best practice for when you have a C#
method parameter that is a List object that is loaded by the method.
It basically is an "out" parameter. Normally I would have the caller
allocate the List object and pass it in such as example 1. However,
if I use example 2, where I declare it an "out" then that has its own
advantages.
EXAMPLE 1:
void mymethod(List<string> abc)
{
If (abc == null)
{
// throw error
}
abc.Clear();
abc.Add("hello");
abc.Add("world");
}
====
versus
=====
EXAMPLE 2:
void mymethod(out List<string> abc)
{
abc = new List<string>();
abc.Add("hello");
abc.Add("world");
}
method parameter that is a List object that is loaded by the method.
It basically is an "out" parameter. Normally I would have the caller
allocate the List object and pass it in such as example 1. However,
if I use example 2, where I declare it an "out" then that has its own
advantages.
EXAMPLE 1:
void mymethod(List<string> abc)
{
If (abc == null)
{
// throw error
}
abc.Clear();
abc.Add("hello");
abc.Add("world");
}
====
versus
=====
EXAMPLE 2:
void mymethod(out List<string> abc)
{
abc = new List<string>();
abc.Add("hello");
abc.Add("world");
}