随机选择两个不同的对象会导致对象引用相同的值

本文关键字:对象引用 对象 选择 两个 随机 | 更新日期: 2023-09-27 18:32:02

我正在尝试编写一个函数,该函数将随机建议在两个城市之间或同一城市之间旅行的起点和目的地,但邮政编码不同。 我有一个类来做这个:

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;
    }
}

我遇到的问题是,当我使用Get City方法选择两个城市时,这两个城市的值相同。 我这样称呼他们:

var random = new Random();
var originCity = CityInfo.GetCity(random);
var destinationCity = CityInfo.GetCity(random);
当我在调用目的地城市获取城市函数

之前设置断点时,原点城市的值与函数调用完成时不同,所以我相当确信起源城市变量引用目的地城市某处我只是不确定为什么或如何解决它

随机选择两个不同的对象会导致对象引用相同的值

在您的

示例中,起始城市与目的地大约一半的时间相同,因为Cities列表中只有两个CityInfo对象。当为起点和目的地选择同一城市时,对象相同,因此GetRandom(city.Zipcodes, random)选择的Zip属性将由 Zip 上设置的目的地调用再次设置。

如果要允许起点和目的地城市是同一CityName但具有不同的Zip则需要创建对象的副本(至少对于原点)。请注意,您最终的起点可能仍随机等于目的地。

如果可以要求起点和终点不同CityNames,则以下代码将CityInfo对象选择随机起点和终点。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stackOverflowRandomRefTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var random = new Random();
            for (int i = 0; i < 10; i++)
            {
                var originCity = CityInfo.GetCity(random, null);
                var destinationCity = CityInfo.GetCity(random, originCity);
                Console.WriteLine(String.Format("orig: {0} {1}",originCity.CityName, originCity.Zip));
                Console.WriteLine(String.Format("dest: {0} {1}",destinationCity.CityName, destinationCity.Zip));
            }
            Console.ReadKey();
        }
    }
    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)
        {
            int n = generator.Next(list.Count());
            Console.WriteLine(n);
            return list.ToArray()[n];
        }
        public static CityInfo GetCity(Random random, CityInfo notThisCity)
        {
            var city = GetRandom(Cities, random);
            while (city == notThisCity)
            {  // if there is only one city this is infinite...
                city = GetRandom(Cities, random);
            }
            city.Zip = GetRandom(city.ZipCodes, random);
            return city;
        }
    }
}