English 中文(简体)
我在这种情况下如何使用代表?
原标题:How would I use a delegate in this scenario?
  • 时间:2012-04-27 03:25:10
  •  标签:
  • c#
  • delegates

一位地雷开发商的朋友告诉我, lo虫使用代表的速度要快得多,而我也希望以此为基准,但我很难把 do连接起来,看它是如何运作的。

考虑以下余额计算器。 如果存在总信用额,并减去每个账户的借项总额,则基本考虑到账户清单并增加初始价值(期初余额):

    private static IDictionary<string, decimal> CalculateBalances(
        IDictionary<string, decimal> initialValue, 
        IDictionary<string, decimal> credits, 
        IDictionary<string, decimal> debits)
    {
        var r = new Dictionary<string, decimal>();

        foreach (var key in initialValue.Select(k => k.Key)
            .Concat(credits.Select(k => k.Key))
            .Concat(debits.Select(k => k.Key))
            .Distinct())
        {
            r.Add(key,
                (initialValue.ContainsKey(key) ? initialValue[key] : 0M)
                + (credits.ContainsKey(key) ? credits[key] : 0M)
                - (debits.ContainsKey(key) ? debits[key] : 0M)
                );
        }

        return r;
    }

这是在中小账户名单上填写的airly,但使用代表将会更快? 坦率地说,代表的逻辑似乎在我思考过程的正确视角下运作,因为我祝贺我领导我如何写这句话。

谁能为利用代表来重写这句话?

最佳回答

我在担任你的朋友时,指的是<代码>ForEach, 载于List<T>等方法。 回答你的简短回答是no

等同合成物是:

initialValue.Select(k => k.Key)
            .Concat(credits.Select(k => k.Key))
            .Concat(debits.Select(k => k.Key))
            .Distinct()
            .ToList()
            .ForEach(var => r.Add(key,
                (initialValue.ContainsKey(key) ? initialValue[key] : 0M)
                + (credits.ContainsKey(key) ? credits[key] : 0M)
                - (debits.ContainsKey(key) ? debits[key] : 0M)
                ));

页: 1 阅读速度缓慢,难度更大。 代表的聘用比普通的聘用方法要慢。 以上各位的辛迪加既更快又容易阅读。

问题回答

谁能为利用代表来重写这句话?

But you are using delegates already! That s what the lambdas are being converted to. The question about whether to use a delegate or not for the loop-body for performance reasons is a little strange when so many delegate invocations are being used just to produce each item of the sequence.

无论如何,亚当· Robinson已经涵盖了你将如何使用<代码>。 清单:每个对清单的每一项目以及相关的可读性和业绩影响执行副作用,因此我胜诉。

但在此,如果准则和代表职责的边际间接费用不是决定因素,我将如何写你的方法:

return initialValue
       .Concat(credits)
       .Concat(debits.Select(kvp => new KeyValuePair<string, decimal>(kvp.Key, -kvp.Value)))
       .GroupBy(kvp => kvp.Key, kvp => kvp.Value)
       .ToDictionary(group => group.Key, group => group.Sum());

现在,这更加可读。

如果您希望使用<代码>Dictionary建立一个新的字典,那么foreach的构造就是正确的。 页: 1 你们总是能够从现有的字典中挑选,以创造新的新字,但会放慢。

然而,就可读性而言,这是否非常容易?

private static decimal GetOrZero(this IDictionary<string,decimal> dict, string key)
{
    decimal value = 0;
    dict.TryGetValue(key, out value);
    return value;
}

private static IDictionary<string, decimal> CalculateBalances(
    IDictionary<string, decimal> initialValue, 
    IDictionary<string, decimal> credits, 
    IDictionary<string, decimal> debits)
{   
    var r = new Dictionary<string, decimal>();
    var accounts = initialValue.Keys.Union(debits.Keys).Union(credits.Keys);

    foreach (var accounts in accounts)
    {
        r.Add(initialValue.GetOrZero(key) + credits.GetOrZero(key) - debits.GetOrZero(key));
    }

    return r;
}




相关问题
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. ...

热门标签