Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View controlflow's full-sized avatar

Alexander Shvedov controlflow

View GitHub Profile
@SLaks
SLaks / UltimateEvil.cs
Last active August 29, 2015 13:56
Run this code as 32 bit and 64 bit, and explain why it crashes on 32-bit
using System;
using System.ComponentModel;
using System.Linq;
class Program {
static void Main(string[] args) {
Console.WriteLine("Attributes: " + typeof(Program)
.GetMembers()
.SelectMany(m => m.GetCustomAttributes(true))
.Count()
@controlflow
controlflow / gist:11d6360ea827164aaa1e
Last active August 29, 2015 14:01
C# 6.0 declaration expressions
// CA "To declaration statement"
{
var t = F(out var x);
// =>
int x;
var t = F(out x);
}
D:\test>csc /o SlowDelegates01.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.34209
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
D:\test>SlowDelegates01.exe
1 => 00:00:00.0159245
2 => 00:00:00.0147080
3 => 00:00:00.0145497
@Porges
Porges / typedtaglessfinal.cs
Created June 17, 2014 21:16
Encoding open recursion in C#: not recommended
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TypedTaglessFinal
{
@RazmikDev
RazmikDev / JetProfile.ps1
Last active November 29, 2015 16:52
PS profile for ReSharper developers (should replace $profile)
# =====================================================================================================================
# Power Plan Management
# ---------------------------------------------------------------------------------------------------------------------
$powerPlans = powercfg -l
$highPerformancePowerPlan = $powerPlans | %{ if ($_.contains("High performance")) { $_.split()[3] }}
$balancedPowerPlan = $powerPlans | %{ if ($_.contains("Balanced")) { $_.split()[3] }}
$powerSaverPowerPlan = $powerPlans | %{ if ($_.contains("Power saver")) {$_.split()[3] }}
$currentPowerPlan = $(powercfg -getactivescheme).split()[3]
function Get-Plan
@leppie
leppie / gist:6935622
Created October 11, 2013 14:28
C# COMPILER, Y U MAKE ME SO MUCH CODE WHEN YOU SHOULD KNOW BETTER?
#if DEBUG
public static XYZ Normalize(this XYZ c, [CallerMemberName] string caller = "")
#else
public static XYZ Normalize(this XYZ c)
#endif
{
#if DEBUG // completely needless
Debug.Print("normalizing: {0} called from: {1}", c, caller);
#endif
return new XYZ
@controlflow
controlflow / gist:7804901
Created December 5, 2013 13:10
How C# lambdas with a single ref-type immutable closure can be optimized
using System;
static class LiftingClosureToDelegateTarget {
static void Main() {
// normal lambda
{
var str = GetString();
Func<string> f = () => str;
Console.WriteLine(f());
}
@controlflow
controlflow / gist:7846397
Last active December 30, 2015 15:09
Boolean solver via C# overload resolution
static class BooleanSolver {
class T {
public static T operator |(T x, T y) => null;
public static T operator |(T x, F y) => null;
public static T operator &(T x, T y) => null;
public static F operator &(T x, F y) => null;
public static F operator !(T x) => null;
}
class F {
class Program
{
// What argument do you need to provide to this method so that it returns true?
public static bool AreYouNuts<T>(T[] array)
{
if (array == null || array.Length == 0)
return false;
var local = (T[]) array.Clone();
using System;
using N; // This using directive is unused, but cannot be simply removed
static class C
{
static void Ex(this string x) { }
static void Foo(Action<string> x, int y) { }
static void Foo(Action<string> x, string y) { }
static void Foo(Action<string> x, object y) { }
static void Foo(Action<int> x, int y) { }