M
mBird
When I pass my List<> without the ref keyword persons2 is empty (Count = 0
below).
If I add the ref keyword to the caller and the function then Count is
correct.
I don't understand because if I call DeSerialize(persons2, path) don't I by
default send a reference not the actual List<Person> since List is an
object?
Thank you!
//The below works since I have the ref keyword...
class Program
{
private const string path = @"c:\temp\Serialization.xml";
static void Main(string[] args)
{
List<Person> persons = new List<Person>();
persons.Add(new Person("Person", "One"));
persons.Add(new Person("Person", "Two"));
Serialize(persons, path);
List<Person> persons2 = new List<Person>();
DeSerialize(ref persons2, path);
Console.WriteLine(persons2.Count.ToString());
Console.ReadLine();
}
static void DeSerialize(ref List<Person> o, string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
StreamReader xmltext = new StreamReader(path, true);
o = (List<Person>)serializer.Deserialize(xmltext);
xmltext.Close();
}
}
below).
If I add the ref keyword to the caller and the function then Count is
correct.
I don't understand because if I call DeSerialize(persons2, path) don't I by
default send a reference not the actual List<Person> since List is an
object?
Thank you!
//The below works since I have the ref keyword...
class Program
{
private const string path = @"c:\temp\Serialization.xml";
static void Main(string[] args)
{
List<Person> persons = new List<Person>();
persons.Add(new Person("Person", "One"));
persons.Add(new Person("Person", "Two"));
Serialize(persons, path);
List<Person> persons2 = new List<Person>();
DeSerialize(ref persons2, path);
Console.WriteLine(persons2.Count.ToString());
Console.ReadLine();
}
static void DeSerialize(ref List<Person> o, string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
StreamReader xmltext = new StreamReader(path, true);
o = (List<Person>)serializer.Deserialize(xmltext);
xmltext.Close();
}
}