I'm trying to write a function that will randomly suggest an origin and a destination to travel between two cities or between the same city but different zip codes. I have a class to do this:
public class CityInfo
{
public string CityName { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public IEnumerable<string> ZipCodes { get; set; }
private static readonly IEnumerable<CityInfo> Cities = new []{
new CityInfo{
CityName = "New York",
State = "NY",
ZipCodes = new[]{"10001", "10002", "10003", "10004", "10005"}
},
new CityInfo
{
CityName = "Washington",
State="DC",
ZipCodes = new []{"20001", "20002", "20003", "20004","20005"}
}
};
private static T GetRandom<T>(IEnumerable<T> list, Random generator)
{
return list.ToArray()[generator.Next(list.Count() - 1)];
}
public static CityInfo GetCity(Random random)
{
var city = GetRandom(Cities, random);
city.Zip = GetRandom(city.ZipCodes, random);
return city;
}
}
The problem I am having is when I go to select two cities with the Get City method both the cities come out to the same value. I'm calling them as follows:
var random = new Random();
var originCity = CityInfo.GetCity(random);
var destinationCity = CityInfo.GetCity(random);
When i set a break point before calling the desination city get city function the value of origin city is different than when the function call completes so I am fairly convinced the originCity variable is referencing destinationCity somewhere I am just not sure why or how to fix it