Skip to content

Instantly share code, notes, and snippets.

@lantoli
Last active August 29, 2015 14:08
Show Gist options
  • Save lantoli/0be7fc63cd1f3e10485a to your computer and use it in GitHub Desktop.
Save lantoli/0be7fc63cd1f3e10485a to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
namespace Reto5
{
public class DictionaryPlus<TK, TV> : Dictionary<TK, TV>
{
public IEnumerable<TV> this[params TK[] keys] {
get {
return keys.Select(key => base[key]);
}
}
}
}
using System;
namespace Reto5
{
public class Duration
{
private readonly Func<DateTime, DateTime> func;
private Duration(Func<DateTime, DateTime> func = null) {
this.func = func;
}
public DateTime From(DateTime date) {
if (func == null)
throw new ArgumentOutOfRangeException();
return func(date);
}
public static explicit operator Duration(int num) {
return new Duration();
}
public static Duration Day {
get {
return new Duration(t => t.AddDays(1));
}
}
public static Duration Week {
get {
return new Duration(t => t.AddDays(7));
}
}
public static Duration Month {
get {
return new Duration(t => t.AddMonths(1));
}
}
}
}
using System;
using System.Runtime.CompilerServices;
namespace Reto5
{
public static class NotNullExtension
{
public static T NotNull<T>(this T obj, string name = null, [CallerMemberName]string caller = "") where T : class
{
if (obj != null) return obj;
var msg = String.Format("{0} was called with null parameter", caller);
throw new ArgumentNullException(name, msg);
}
}
}
using System;
namespace Reto5
{
// MUST BE COMPILED WITH: /unsafe
public static class ToUpperNoCopyExtension
{
public unsafe static void ToUpperNoCopy(this string obj) {
if (obj == null) throw new ArgumentNullException();
fixed (char* p = obj) {
for (int i = 0; i < obj.Length; i++) {
p[i] = Char.ToUpper(p[i]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment