Skip to content

Instantly share code, notes, and snippets.

View DForshner's full-sized avatar

David Forshner DForshner

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackAllocVsHeapAlloc
{
public static class Sandbox
@DForshner
DForshner / CreateNewInstanceOfClassByNameUsingLookup.cs
Last active December 23, 2015 13:39
Create a new instance of class by name (string) using a look up table.
// Creates new instance of a class by name (string).
public IClass GetDataSource(string className)
{
// Should handle case where the className does not exist in dictionary.
// Lookup table that maps class string names to a constructor action.
var classDictonary = new Dictionary<string, Func<IClass>>
{
{ "ClassA", () => new ClassA()},
{ "ClassB", () => new ClassB()},
@DForshner
DForshner / Linq2EntitySum.cs
Created September 18, 2013 22:41
Using Sum() with Linq-2-Entities navigation properties.
// Fixes error: The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.
// In the case where none of the navigation property's StringPropA properties match a null will returned.
// .Sum() will throw an error if you try to sum null so we replace the null with a default value using .DefaultIfEmpty()
var total = Parent
.Where(x => x.NavigationProperty.StringPropA == "Foo")
.Select(x => x.DecimalPropB)
.DefaultIfEmpty(0)
.Sum()
@DForshner
DForshner / ListDuplicatesToCSV.cs
Created September 13, 2013 17:25
Converting any duplicates in a list into a CSV string.
var duplicates = this.primaryJobs
.Select(x => new { Foo = x.Bar })
.GroupBy(x => x.Foo)
.Select(x => new { Foo = x.Key, Count = x.Count() })
.Where(x => x.Count > 1)
.Select(x => x.Foo)
.Aggregate((i,j) => i + ',' + j);
// "A, B, C"
@DForshner
DForshner / NLogConfig.cs
Last active April 9, 2023 01:01
Programmatically configure NLog
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Diagnostics;
namespace Web.App_Start
{
/// <summary>
/// Programmatically configure NLog.
@DForshner
DForshner / RedirectToHTTPSAttribute.cs
Last active March 21, 2017 16:44
MVC attribute that redirects non HTTPS requests to a HTTPS URI.
using System;
using System.Web;
using System.Web.Mvc;
namespace Infrastructure
{
/// <summary>
/// MVC attribute that redirects non HTTPS requests to a HTTPS URI.
/// </summary>
public class RedirectToHTTPSAttribute : ActionFilterAttribute
@DForshner
DForshner / RequireHttpsAttribute.cs
Created July 27, 2013 14:23
A Web API filter that requires requests to be HTTPS.
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace Infrastructure
{
/// <summary>
@DForshner
DForshner / ImplementingICloneable.cs
Created July 4, 2013 15:18
Implementing ICloneable
public class ImplementingICloneable : ICloneable
{
/// <summary>
/// Returns a clone of the current object.
/// </summary>
public ImplementingICloneable Clone()
{
return (ImplementingICloneable)this.MemberwiseClone(); // MemberwiseClone() only creates a shallow copy.
}
@DForshner
DForshner / GetUserNameOfAccountRunningUnitTest.cs
Created July 3, 2013 19:29
Gets the username of the account running the unit test.
[TestMethod]
public void Name_When_Expect()
{
// Arrange
var userName = Environment.UserName;
// Act
// Assert
}
static const int values[] = {0,1,2,3,4,5,6,7,8,9};
vector<int> v (values, values + sizeof(values) / sizeof(values[0]) );
// Using C++11 ranged-based for loop
std::stringstream ss;
for (int n : v)
ss << n;
Assert::IsTrue(ss.str() == "0123456789");
ss.str(""); ss.clear(); // clear string stream