The out parameter must be set so that the control leaves the current methods.

I am very new C # and this is the first time I am doing anything with a list, so this can be a very stupid question ... I am trying to read data from a file into a list that consists of objects Tourist. As far as I understand, I need to assign something to the list touristsbefore adding objects to it, but I'm not sure how to do it.

class Tourist
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double Contributed { get; set; }

    public Tourist(string firstName, string lastName, double money)
    {
        FirstName = firstName;
        LastName = lastName;
        Contributed = money * 0.25;
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Tourist> tourists = new List<Tourist>();

        ReadData(out tourists);
    }

    static void ReadData(out List<Tourist> tourists)
    {
        const string Input = "..\\..\\Duomenys.txt";

        string[] lines = File.ReadAllLines(Input);
        foreach (string line in lines)
        {
            string[] values = line.Split(';');
            string firstName = values[0];
            string lastName = values[1];
            double money = Double.Parse(values[2]);
            tourists.Add(new Tourist(firstName, lastName, money));
        }
    }
}
+4
source share
3 answers

By declaring a parameter out, you will “promise” the caller (and the compiler) that your method will set the value of the variable provided as an argument to that parameter.

, .

tourists. NullReferenceException at tourists.Add(...), null.

, out tourists Main. , ReadData , , tourists. ( ), out.

, ReadData ,

tourists = new List<Tourist>()

ReadData foreach.


, ommit ReadData, :

static List<Tourist> ReadData()
{
    // create list
    List<Tourist> tourists = new List<Tourist>();

    const string Input = "..\\..\\Duomenys.txt";       

    string[] lines = File.ReadAllLines(Input);
    foreach (string line in lines)
    {
        // shortened for brevity
        tourists.Add(new Tourist(firstName, lastName, money));
    }

    return tourists; // return newly created list
}

Main :

static void Main(string[] args)
{
    List<Tourist> tourists = ReadData();
}
+5

out . msdn:

, , , .

:

-, :

static void Main(string[] args)
{
    List<Tourist> tourists;
    ReadData(out tourists);
}

static void ReadData(out List<Tourist> tourists)
{
    tourists = new List<Tourist>();
    //...
}

-, , out , .

( IMO ) , out:

static void Main(string[] args)
{
    List<Tourist> tourists = ReadData();
}

static List<Tourist> ReadData()
{
    List<Tourist> tourists = new List<Tourist>();
    //...
    return tourists;
}
+2

out -.
static void ReadData(out List<Tourist> tourists)
{
    const string Input = "..\\..\\Duomenys.txt";
    tourists = new List<Tourist>();

    string[] lines = File.ReadAllLines(Input);
    foreach (string line in lines)
    {
        string[] values = line.Split(';');
        string firstName = values[0];
        string lastName = values[1];
        double money = Double.Parse(values[2]);
        tourists.Add(new Tourist(firstName, lastName, money));
    }
}

, List<T>, , out.keyword , , .

, ReadData , , :

var tourists = new List<Tourist>();
ReadData(toursists);  // this changes the elements within the list
// do something with the list
+1

Source: https://habr.com/ru/post/1654461/


All Articles