Skip to content

Instantly share code, notes, and snippets.

@btjake
btjake / NotNull.cs
Last active August 29, 2015 14:04
a wrapper that can never be null
public struct NotNull<T>
{
private T _value;
public T Value {
get
{
return _value;
}
set
{
@btjake
btjake / Test.cs
Created July 25, 2014 16:08
Dictionary vs ConcurrentDictionary Performance
System.Collections.Generic.Dictionary<int, string> regularDictionary = new System.Collections.Generic.Dictionary<int, string>();
System.Collections.Concurrent.ConcurrentDictionary<int, string> concurrentDictionary = new System.Collections.Concurrent.ConcurrentDictionary<int, string>();
void Main()
{
for(int i = 10000; i < 10000000; i *= 10)
{
regularDictionary = new System.Collections.Generic.Dictionary<int, string>();
concurrentDictionary = new System.Collections.Concurrent.ConcurrentDictionary<int, string>();
Wrapper(TestRegularDictionaryWrite, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} regular write", i));
@btjake
btjake / CurryAddUpdateExtension.cs
Created January 28, 2014 21:50
A curry (sorta) function for adding/updating a Dictionary<TKey, List<TValue>>
public static class Extensions
{
public static Action<TKey, TValue> CurryAddUpdate<TKey,TValue>(this Dictionary<TKey, List<TValue>> dict)
{
Action<TKey, TValue> op = (key, val) => {
if(dict.ContainsKey(key))
{
dict[key].Add(val);
} else {
dict.Add(key, new List<TValue>(new TValue[]{val}));
@btjake
btjake / attributes.js
Created January 24, 2014 20:27
Get a list of all attributes for a given selector
(function($, selector)
{
var coll = {};
$(selector).map(function(ei, ev) {
if(typeof ev.attributes != "undefined") {
$.map(ev.attributes, function(av, ai) {
if(!coll[av.name]) coll[av.name] = true;
});
}
});
@btjake
btjake / ServiceStack.cs
Last active December 30, 2015 20:09
An example of what I'm trying to do w/ServiceStack
public class HelloService : Service
{
public object NamedMethod(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
public class HelloResponse
{
@btjake
btjake / DictionaryExtension.cs
Created December 9, 2013 16:00
AddOrUpdate method for IDictionary since none exists for some reason
void Main()
{
Dictionary<int, int> dict = new Dictionary<int, int>();
Random rndm = new Random();
for(int i = 0; i < 100; i++)
{
int foo = rndm.Next(10);
dict.AddOrUpdate(foo, 1, (val) => { return val+=1; });
}
@btjake
btjake / CoolKidsClass.cs
Last active December 15, 2015 11:39
Optional in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OptionalParams
{
public class CoolKidsClass
{
@btjake
btjake / ObjectSize.cs
Created March 21, 2013 02:16
they see me trollin...
using System;
using System.Collections;
namespace ObjectSize
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(SizeOfInstantiatedObjects());