Skip to content

Instantly share code, notes, and snippets.

@dfmartin
dfmartin / uglyPropertyDefault.cs
Last active August 27, 2015 03:55
Ugly property defaults
private string _firstName = "John";
private string _lastName = "Doe";
public string FirstName {
get { return _firstName; }
set { _firstName = value; }
}
public string LastName {
get { return _lastName; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; } = "John";
public string LastName { get; set; } = "Doe";
public class CurrentSession {
public CurrentSession() {
CreateDate = DateTime.Now;
}
public DateTime CreateDate { get; }
}
var x = People[0].Address.City;
// oops! Null reference exception. Need to replace with. . .
string x = string.Empty;
if(People != null) {
var p = People[0];
if (p != null) {
var a = p.Address;
if (a != null) {
x = a.City;
@dfmartin
dfmartin / gist:5703425
Created June 4, 2013 03:44
Comparing Type with GenericType to determine if a type is derived from a generic type where the generic argument type is unknown.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.IsTrue(typeof(Foo<>).IsAssignableFrom(typeof(Bar))); // fails
}
[TestMethod]
@dfmartin
dfmartin / prepare-commit-msg
Created May 13, 2016 13:56
Prepare-commit-msg
# Currently in .git/hooks. Also check .git_template/
#!/bin/bash
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
@dfmartin
dfmartin / .gitconfig
Last active August 23, 2017 16:18
git aliases
##
[alias]
co = checkout
cob = checkout -B
fp = fetch --prune
# log commands
lsg = log --all --graph --oneline --decorate
ls = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
function snakeToCamel(str){
return str.toLowerCase()
// Replaces any - or _ characters with a space
.replace( /[-_]+/g, ' ')
// Removes any non alphanumeric characters
.replace( /[^\w\s]/g, '')
// Uppercases the first character in each group immediately following a space
// (delimited by spaces)
.replace( / (.)/g, function($1) { return $1.toUpperCase(); })
// Removes spaces
@dfmartin
dfmartin / async-sleep.ts
Created September 27, 2019 20:53
typescript async sleep
const sleep = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms));
};