Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
thomaslevesque / CSharpErrorsAndWarnings.md
Last active April 11, 2024 21:35
All C# errors and warnings. NOTE: this list is out of date. See https://github.com/thomaslevesque/GenerateCSharpErrors/blob/master/CSharpErrorsAndWarnings.md for a more recent version

All C# errors and warnings

Parsed from the Roslyn source code using Roslyn.

Code Severity Message
CS0006 Error Metadata file '{0}' could not be found
CS0009 Fatal Metadata file '{0}' could not be opened -- {1}
CS0012 Error The type '{0}' is defined in an assembly that is not referenced. You must add a reference to assembly '{1}'.
CS0016 Error Could not write to output file '{0}' -- '{1}'
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
namespace WpfApplication1
{
public class AsyncObservableCollection<T> : ObservableCollection<T>
{
private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
@thomaslevesque
thomaslevesque / .gitignore
Last active November 3, 2022 07:56
Tests in same project
bin/
obj/
.vs/
.idea/
.vscode/
*.user
@thomaslevesque
thomaslevesque / NaturalSortStringComparer.cs
Created September 14, 2022 01:45
Natural sort string comparer
using System;
using System.Collections.Generic;
/// <summary>
/// String comparer that takes numbers into account.
/// e.g. "Episode 9" will be considered less than "Episode 10", even though it would be greater based on strictly
/// alphabetical order.
/// </summary>
public class NaturalSortStringComparer : IComparer<string>
{
@thomaslevesque
thomaslevesque / gitconfig
Last active March 28, 2022 12:54
Useful git aliases
[repo]
defaultRemoteName = origin
mainBranchName = master
[alias]
diffc = diff --cached
logg1 = log --graph --oneline
current-branch = rev-parse --abbrev-ref HEAD
publish = !git push -u $(git config repo.defaultRemoteName) $(git current-branch)
finish-branch = !branchName=$(git current-branch) && git fetch $(git config repo.defaultRemoteName) $(git config repo.mainBranchName):$(git config repo.mainBranchName) && git checkout $(git config repo.mainBranchName) && git branch -d $branchName && git fetch -p $(git config repo.defaultRemoteName)
async void Main()
{
Console.WriteLine ("Before Using");
await Async.Using(new Test(), t =>
{
Console.WriteLine ("In Using body");
throw new Exception("Oops");
});
Console.WriteLine ("After Using");
}
@thomaslevesque
thomaslevesque / CSharpErrorCodes.cs
Created May 10, 2017 01:10
Generate "warnings as errors" ruleset from error code definitions in Roslyn source code
void Main()
{
string errorCodesFileUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs";
string errorCodesFileContent = new WebClient().DownloadString(errorCodesFileUrl);
var syntaxTree = CSharpSyntaxTree.ParseText(errorCodesFileContent);
var root = syntaxTree.GetRoot();
var enumDeclaration =
root.DescendantNodes()
.OfType<EnumDeclarationSyntax>()
.First(e => e.Identifier.ValueText == "ErrorCode");
@thomaslevesque
thomaslevesque / memset.cs
Last active July 6, 2021 01:48
Benchmark of memset implementations in C# using a loop or the initblk CLR instruction (run in LinqPad)
void Main()
{
var array = new byte[10000000];
var initBlk = new InitblkMemoryHelper();
var loop = new LoopMemoryHelper();
// First run for JIT warmup and type initialization
initBlk.Memset(array, 0, array.Length, 42);
loop.Memset(array, 0, array.Length, 42);
@thomaslevesque
thomaslevesque / ParseAuthChallenge.cs
Created February 23, 2017 00:56
Parsing HTTP authentication challenge with Sprache
void Main()
{
ParseAndPrintChallenge(@"Bearer realm=""FooCorp"", error=invalid_token, error_description=""The access token has expired""");
}
void ParseAndPrintChallenge(string input)
{
var challenge = Grammar.Challenge.Parse(input);
Console.WriteLine($"Scheme: {challenge.Scheme}");
Console.WriteLine($"Parameters:");
@thomaslevesque
thomaslevesque / GitHubMarkdownShortcuts.user.js
Last active April 14, 2021 00:33
Keyboard shortcuts for GitHub markdown editor
// ==UserScript==
// @name GitHub Markdown shortcuts
// @namespace ThomasLevesque
// @include https://github.com/*
// @include https://gist.github.com/*
// @version 1
// @grant none
// ==/UserScript==