Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
JohanLarsson / Subsets
Last active December 18, 2015 00:38
public static class ListExt
{
public static IEnumerable<T[]> GetSubsets<T>(this List<T> set, int subsetLength)
{
int[] indices = Enumerable.Range(0, subsetLength).ToArray();
while (true)
{
yield return indices.Select(i => set[i]).ToArray();
indices = Increment(indices, set.Count - 1);
if (indices == null)
public static class StopwatchExt
{
public static string GetTimeString(this Stopwatch stopwatch, int numberofDigits = 1)
{
double s = stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
if (s > 1)
return Math.Round(s, numberofDigits) + " s";
if (s > 1e-3)
return Math.Round(1e3 * s, numberofDigits) + " ms";
if (s > 1e-6)
public static class ListEx
{
public static IEnumerable<T> WrappingEnumerator<T>(this List<T> list)
{
var i = 0;
while (true)
{
yield return list[i];
i = (i < (list.Count - 1))
? i + 1
using System.Collections.Generic;
using NUnit.Framework;
class LinqExtTests
{
[TestCase(0, 0, 0, 0)]
[TestCase(1, 0, 0, 0)]
[TestCase(0, 1, 0, 1)]
[TestCase(0, 0, 1, 2)]
public void MaxByTest(int first, int second, int third, int expectedIndex)
{
public class PerformanceTests
{
[Test]
public void ReadSpeedTest()
{
var excelApp = new Application();
var workbook = excelApp.Workbooks.Add();
var sheet = (Worksheet)workbook.Worksheets[1];
var ns = new[] { 10, 100, 1000, 10000 };
BatchWrite(sheet, ns.Max());
public class InternetConnection
{
/// <summary>
/// Check if internetconnection is available
/// </summary>
/// <param name="lpdwFlags">Returns info about connection</param>
/// <param name="dwReserved">Reserved pass 0 (default)</param>
/// <returns></returns>
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved = 0);
@JohanLarsson
JohanLarsson / CreateListView Xaml
Last active December 18, 2015 23:29
Generates the Xaml if you want quick and dirty
[TestCase(typeof(Stats), "StatsProp")]
public void CreateListView(Type type, string bindingPopertyName)
{
PropertyInfo[] propertyInfos = type.GetProperties();
var listView = new XElement("ListView");
listView.Add(new XAttribute("ItemsSource", string.Format(@"{{Binding {0}}}", bindingPopertyName)));
var view = new XElement("ListView.View");
var gridView = new XElement("GridView");
foreach (var propertyInfo in propertyInfos)
{
public static class AttachedProperties
{
public static readonly DependencyProperty IconProperty = DependencyProperty.RegisterAttached(
"Icon",
typeof (StreamGeometry),
typeof (AttachedProperties),
new PropertyMetadata(default(StreamGeometry))
);
public static void SetIcon(UIElement element, StreamGeometry value)
<UserControl x:Class="Namespace.FileControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Grid Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;