English 中文(简体)
c 具有相同论点的排他性功能,在多重运行[封闭式]方面回报不同的结果
原标题:c# function with same arguments return different results on multiple runs [closed]

我有一只剪切功能,即作为更大的 al子的一部分,但设计这一功能时却犹豫不决。 正在根据同样的论点,计算不同结果,以适合多个方面。 我看不到问题在什么地方发挥作用。 任何开明的建议都会受到欢迎。

private readonly Dictionary<int,decimal>_cache = new Dictionary<int, decimal>(); // lookup cache
    private void CalculateFitness(TimeTable timeTable)
    {                      
        const int points = 1;            

        var exams = timeTable.Exams.ToList();
        var combinations = exams.Select(x => exams.Where(y => exams.IndexOf(y) > exams.IndexOf(x))
                                                          .Select(z => new List<Exam> { x, z }))
                                                          .SelectMany(x => x);


        var clash = combinations.Where(touple => touple[0].Period.Id == touple[1].Period.Id && touple[0].Date == touple[1].Date && touple[0].Students.Intersect(touple[1].Students).Any());
        var clCount = clash.Sum(touple => touple[0].Students.Intersect(touple[1].Students).Count());            



        var score = clCount == 0 ? timeTable.Exams.Count : timeTable.Exams.Count - clCount;


        if (_cache.ContainsKey(score))
        {
            timeTable.Fitness = _cache[score];
        }
        else
        {
            timeTable.Fitness = Math.Abs(decimal.Divide(score, (timeTable.Exams.Count * points))); // Calculate Fitness 
            _cache.Add(score, timeTable.Fitness);
        }
    }
最佳回答

It is returning different results on multiple runs for the same arguments

这意味着你的职能不是免费的,你正在改变或利用全球国家。 实现和取消这些职能,应始终保持预期价值。

问题回答

这一职能似乎依靠同样的动态数据。 方法机构将帮助很多人。

你们认为,不可能有相同论点的不同结果? C#毕竟不是实用语言。

这个问题的另一个传统是:

foreach (var foo in myFooList)
{
    DoSomethingAsync(() => { SomeFunctionToExecute(foo); }
}

(进入经修改的关闭)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签