Skip to content

Instantly share code, notes, and snippets.

using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
if(args is []) {
args = ["--filter", "*"];
}//if
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
/* Debugged application */
using System;
static class Program
{
static void Main(string[] args) {
Console.WriteLine(args?.Length); // Step into Specific, Console.WriteLine here
}
}
// Before
var dictionary1 = new Dictionary<int, string> {
{ 1, "One" },
{ 2, "Two" },
};
// After
var dictionary2 = new Dictionary<int, string> {
[1] = "One",
[2] = "Two",
using System;
class Program
{
static void Main() {
var thing = new Thing();
if(thing.A && thing == null) {
Console.WriteLine("thing == null");
}//if
}
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
{
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;
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[]>();
// 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;
class Program
{
static void Main() {
var x = DateTime.Now.Day > 10 ? 1 : new Program() ?? default(object);
}
}
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!