Why double down. The right success of calls depends on Console.WriteLine or delay?

I have the following class:

public class Point {

    public Point(string latitude, string longitude)
    {
        Longitude = double.Parse(longitude.ToString());
        Latitude = double.Parse(latitude.ToString());
    }

    public double Longitude { get; set; }

    public double Latitude { get; set; }
}

I call it in a loop with data read from an ASCII encoded file.

In the above form, for each iteration of the loop when creating an instance of the class using

var p = new Geocode.Point(string1, string2);

the constructor fails with the following exception

System.FormatException: Input string was not in a correct format.
    at System.Number.ParseDouble(String value, NumberStyles options,
       NumberFormatInfo numfmt)
    at System.Double.Parse(String s)

However, if I changed the constructor for Pointto the following:

public Point(string latitude, string longitude)
    {
        Console.WriteLine("Latitude: " + latitude);
        Console.WriteLine("Longitude: " + longitude);
        Longitude = double.Parse(longitude.ToString());
        Latitude = double.Parse(latitude.ToString());
    }

and run the loop, double.Parsesucceed.

Adding Thread.Sleep(100);Console.WriteLine instead of calls in the same way ensures that the cycle works without problems, so it’s obvious that there is a problem with synchronization, but why not in this particular case, given that string1they string2are passed by value, not links in both situations?

+4

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


All Articles