Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

7

if (lookup.ContainsKey(transmitter)) { var receiverLookup = lookup[transmitter]; This searches for the KeyValuePair twice. There's a more efficient approach: if (lookup.TryGetValue(transmitter, out var receiverLookup)) var ungrouped = receivers.Except(blocksTracker).ToArray(); foreach (var receiver in ungrouped) ...


7

Calling ElementAt inside a loop will cause the IEnumerable to be called multiple times. This code isn't deffering execution either. Now if you think the pervious code wasn't readable then hold on to your hats. I'm going to post some code in hopes someone can build on it and maybe come up with something better. public static class IEnumerableExtensions {...


3

Another opportunity to solve this problem is to use Transpose() in MoreLinq with Linq itself: var listoflists = new List<List<string>>() { one, two, three, four, five }; var res = listoflists.Transpose() .SelectMany(x => x) .Take(10); Result: { "aaa", "eee", "ggg", "lll", "1111", "bb", "fff", "hhhhh", "22", "ccc" }


3

A few comments about the API which completely ignore the spec: I would rename N to count, which is descriptive and follows typical naming conventions. Your method would benefit from inline documentation (///), which could clarify the behaviour (what does Extract mean?!?) and describe the parameters precisely. It's good that you've used the general-purpose ...


3

CharlesNRice's solution has still one flaw. It eagerly enumerates the main collection and since we don't know its length, we better avoid it. Since the other answer already mentions flaws in your code, let me just add this lazy alternative. It's a little bit tricky to make it deferred because you first need to enumerate the main collection and create ...


2

Correctness if( array.Length <= 0 ) return 0; This piece of code is suspicious. Maybe you should return sbyte.MaxValue, maybe null, or maybe throw ArgumentException. (I can imagine that there were a Max method, both, together is used to find the range of values, then maybe 0 is a valid return value.) This is one of the rare times I'd appreciate a ...


1

Frame challenge I was trying to avoid "complexity", so get rid of 3rd part libs Although it's not strictly code review, I would like to start by challenging this goal. There are many many official Microsoft libraries with dependencies on Newtonsoft.Json. (See this answer on our software engineering sister site). Core code public static string ...


1

just modified the as like string extension public static string GetDescription<T>(this T value) where T : IConvertible { return (value is Enum) ? value .GetType() .GetMember(value.ToString()) .FirstOrDefault() ?.GetCustomAttribute<DescriptionAttribute>() ...


1

You could allow for some more flexibility in type conversions by using type converters. public static T Get<T>(this IDictionary<string, object> dictionary, TypedKey<T> key) { return (T)Convert.ChangeType(dictionary[key.Name], typeof(T)); } static class DictionaryExtensions { public static T Get<T>(this ...


Only top voted, non community-wiki answers of a minimum length are eligible