Skip to content

Instantly share code, notes, and snippets.

public static class Sequence
{
public static IEnumerable<T> Memoize<T>(this IEnumerable<T> sequence)
{
return new MemoizedSequence<T>(sequence);
}
}
// This code is for illustration and is not production-ready.
@thomaslevesque
thomaslevesque / ThemeInfo.cs
Created September 6, 2012 08:33
Get information about current Windows theme in C#
public class ThemeInfo
{
private readonly string _themeName;
private readonly string _themeColor;
private readonly string _themeSize;
private readonly string _themeFileName;
public ThemeInfo(string name, string fileName, string color, string size)
{
_themeName = name;
void Main()
{
var list1 = Enumerable.Range(0, 10000).Select(i => i.ToString());
var list2 = Enumerable.Range(0, 10000).Select(i => i.ToString());
var list3 = Enumerable.Range(0, 10000).Select(i => i.ToString());
// First run to avoid JIT during the benchmark
list1.ZipOne(list2, list3, Merge).Count();
list1.ZipTwo(list2, list3, Merge).Count();
@thomaslevesque
thomaslevesque / DelegateCommand.cs
Created April 17, 2013 23:14
Basic DelegateCommand implementation
using System;
using System.Windows.Input;
namespace QuestionableContent.Input
{
public class DelegateCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
@thomaslevesque
thomaslevesque / Strings.tt
Last active February 5, 2021 15:06
Generates a strongly typed wrapper for a resource file, compatible with the Portable Class Library. Just drop this T4 template in your project, with the same base name as a .resx file in the same folder (e.g. Strings.tt for Strings.resx)
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Collections.Generic" #>
@thomaslevesque
thomaslevesque / DataProtectionExtensions.cs
Created May 26, 2013 14:40
Simple string encryption using Data Protection API in WinRT
using System;
using System.Threading.Tasks;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.DataProtection;
namespace App1
{
public static class DataProtectionExtensions
{
public static async Task<string> ProtectAsync(this string clearText, string scope = "LOCAL=user")
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 / gist:6100447
Created July 28, 2013 22:05
Proof that enum types are mutable
var field = typeof(DayOfWeek).GetField("value__");
var d = DayOfWeek.Monday;
Console.WriteLine (d);
TypedReference r = __makeref(d);
field.SetValueDirect(r, 4);
Console.WriteLine (d);
@thomaslevesque
thomaslevesque / Distinct2.cs
Last active December 20, 2015 09:19
Distinct overload with 2 type parameters, to solve the problem shown on Eric Lippert's blog (http://ericlippert.com/2013/07/29/a-contravariance-conundrum/)
public static IEnumerable<T> Distinct<T, U>(this IEnumerable<T> source, IEqualityComparer<U> comparer)
where T : U
{
IEqualityComparer<T> comparer2 = (IEqualityComparer<T>)comparer;
return Enumerable.Distinct<T>(source, comparer2);
}
@thomaslevesque
thomaslevesque / AlternateDataStream.cs
Created August 12, 2013 13:00
Class that gives access to NTFS Alternate Data Streams
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace ADS
{
public static class AlternateDataStream
{
public static FileStream Open(string path, string streamName, FileMode mode)
{