Skip to content

Instantly share code, notes, and snippets.

@ViIvanov
ViIvanov / Count(), Any().cs
Last active January 4, 2016 14:58
Count(), Any()
void M<T>(ICollection<T> source) {
var a = source.Any(); // OK, statically known, that source is ICollection<T> and call is optimized
var b = source.Count(); // Warning, source.Count is better
}
bool M1<T>(IEnumerable<T> source) {
return source.Any(); // OK - one iteration in a worst case
}
int M1<T>(IEnumerable<T> source) {
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
namespace Xxx.ComponentModel
{
public abstract class CustomPropertyDescriptor : PropertyDescriptor
{
protected CustomPropertyDescriptor(MemberDescriptor descr) : base(descr) { }
using System.Collections.Generic;
using System.Collections.ObjectModel;
internal static class Program
{
private static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key) { return default(TValue); }
private static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key) { return default(TValue); }
private static void Main() {
var dictionary = new Dictionary<int, int>();
static class C
{
static object M0<T>(IEnumerable<T> source) {
// linq с новой строки
var e1 =
from item in source
select item.ToString();
return e1;
}
void Method(Entity entity) {
if(entity == null) {
throw new ArgumentNullException(nameof(entity));
} else if(entity.Some.Property > 0) {
throw new ArgumentException(stringanize(entity.Some.Property > 0), nameof(entity));
}//if
// "stringanize" makes a string from an expression
// and when we are renamed "Some" or "Property"
// string is updated!
using System;
class Program
{
static void Main() {
var x = DateTime.Now.Day > 10 ? 1 : new Program() ?? default(object);
}
}
// Overload of Debug.Assert with detailMessageFormat does not supported
// Debug.Assert has an overload with detailMessageFormat parameter: https://msdn.microsoft.com/en-us/library/cc190597(v=vs.110).aspx.
// This overload does not supported by R#:
// * No syntax highlighting and no validation of format arguments
// * Code analysises does not recognize this calls (null check not works)
class Program
{
using System;
using System.Collections.Generic;
static class Program
{
static void Add(IReadOnlyCollection<object> items) { }
static void Add(IEnumerable<IReadOnlyCollection<object>> items) { }
static void Main() {
var list = new List<object[]>();
using System;
using System.Linq;
static class Program
{
static void Main(string[] args) {
var lengths = args.Select(item => item.Length);
// "lengths" can be enumerated a few times, depending on a size of "args".
var xxx = args.Select(item => lengths.Select(x => x));
}
using System.Collections.Generic;
// ReSharper disable once CheckNamespace
class MultiDictionary<TKey, TValue> : Dictionary<TKey, IList<TValue>>
{
public void Add(TKey key, params TValue[] values) { }
}
static class Program
{