New answers tagged

1

Your implementation doesn't seem to bother if the RNA-sequence contains invalid characters like: "UXGUGUUAUUA". Is that on purpose? I think, I would expect an exception or at least some reporting in a log. An alternative to a switch-statement is often a dictionary - especially if the cases are going to vary or maybe should be localized - because a ...


2

If you want a one-pass, then you can do something like this: public static string[] Proteins(string strand) { return GetProteins(strand).ToArray(); } private static IEnumerable<string> GetProteins(string strand) { if (string.IsNullOrEmpty(strand)) { throw new ArgumentNullException(nameof(strand)); } for (var i = 0; i < strand.Length; ...


0

I would prefer to use C# 7 Value Tuples: private static IEnumerable<(string Key, string Value)> GetPropertyValues(MyClass myClass) { return PropertyDictionary .Select(kvp => ( kvp.Key, Value:(string)kvp.Value.GetValue(myClass, null) ) ) .Where(...


5

Because the question is about performance and efficiency that's why some sort of benchmarking would be essential to compare different implementations. I have found BenckmarkDotNet really useful for these kind of experiments. You can define the for loop version as your baseline and the tool will compare the other implementation against that. Here is my ...


3

If you're using C#6 or above, you can use the null conditional operator ?.: var hasCreator = GetSubPrivilagesFromPrivilageCodeStatic("Authoring01")?.SubprivilagesList?.Any(cd => cd.PrivilageCode == "CREATOR") ?? false; if (hasCreator) { objTemplate.IsTagged = true; } If it's not too late, I'd correct the spelling of privilege in your code. I ...


2

The only think that comes to my mind is to rewrite this: var item = subPrivilagesResponses.SubprivilagesList.SingleOrDefault(cd => cd.PrivilageCode == "CREATOR"); if(item != null && item.PrivilageCode == "CREATOR") objTemplate.IsTagged = true; into this: if (subPrivilagesResponses.SubprivilagesList.Any(cd => cd.PrivilageCode =...


3

Just looking at your tests, there's a couple of points to consider... Naming Having Test at the front of every test case is usually redundant (public methods in test classes are tests...). The beginning of the test name is also quite valuable real-estate since your test runner/window is likely to truncate what it displays after a certain number of ...


3

Slepic and Henrik are wondering about the use of foreach and enumerator, and I'm too. Anyway, instead of having different versions with actual implementations for the same purpose (count the distinct elements), you can create one private method with the full implementation, and then, just call back this method on the other methods. So, the main ...


5

As slepic in his comment, I also wonder why you use an enumerator in the first and foreach in the second place? You can eliminate null checks in the versions that call other overrides: public static int DistinctCount<TSource>(this IEnumerable<TSource> source) => source?.DistinctCount((IEqualityComparer<TSource>)null) ?? throw ...


Top 50 recent answers are included