Skip to content

Instantly share code, notes, and snippets.

View dcagnetta's full-sized avatar

Dillan Cagnetta dcagnetta

View GitHub Profile
@dcagnetta
dcagnetta / ReadOnlyStructs.cs
Last active March 18, 2020 11:27
Read only structs with tuple initializer
public readonly struct Point
{
public double X { get; }
public double Y { get; }
public double Distance { get; }
public Point(double x, double y) =>
(X, Y, Distance) = (x, y, x * y);
public static bool operator == (Point left, Point right) =>
@dcagnetta
dcagnetta / NullDiscardCheck.cs
Created March 18, 2020 11:32
Discard null check
public void Check(Person person)
{
// _ : variable that is thrown away by compiler
_ = person ?? throw new ArgumentNullException(nameof(person));
// code here ...
}
@dcagnetta
dcagnetta / SwitchExpressions.cs
Last active March 18, 2020 11:48
Switch Expressions with Tuples
public class Program
{
public static void Main()
{
Console.WriteLine(PeakTimeCalculator(DateTime.Now, true));
Console.WriteLine(PeakTimeCalculator(DateTime.Now.AddMonths(3), false));
}
// Calculates costs based on month and whether or not its peak set
public static decimal PeakTimeCalculator(DateTime date, bool isPeak) =>
@dcagnetta
dcagnetta / button-stream.ts
Last active April 30, 2020 11:56
Observable button click stream
// imports
import { Observable, fromEvent } from 'rxjs';
// variables
btnApplyClicks$: Observable<any>;
@ViewChild('btnApply', { static: false }) btnApply: MatButton;
// ngAfterViewInit
this.btnApplyClicks$ = fromEvent(this.btnApply._elementRef.nativeElement, 'click');
@dcagnetta
dcagnetta / http_request.cs
Created July 15, 2020 02:07
Httpclient request with custom headers
using Newtonsoft.Json;
...
var client = new HttpClient();
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.clickatell.com/rest/message"),
Headers = {
{ HttpRequestHeader.Authorization.ToString(), "Bearer xxxxxxxxxxxxxxxxxxxx" },
@dcagnetta
dcagnetta / types.ts
Last active March 15, 2021 13:20
Typescript Advanced types creation and usage
type SearchParams = {
title?:string;
publishYear?:string;
author?:string;
}
type Book = {
isbn:string;
title:string;
publishYear:string;
@dcagnetta
dcagnetta / dynamic_cast.cs
Last active September 4, 2020 07:34
Dynamic Casting in C#
using System;
using System.ComponentModel;
public class Program
{
public static void Main()
{
var dataType = typeof(Double);
var converter = TypeDescriptor.GetConverter(dataType);
@dcagnetta
dcagnetta / chocolatey.bat
Last active July 28, 2023 06:37
PC Setup scripts
choco install git -y
choco install git-credential-manager-for-windows -y
choco install notepadplusplus -y
choco install nodejs-lts -y
choco install slack -y
choco install docker-desktop -y
choco install f.lux -y
choco install vscode -y
choco install googlechrome -y
choco install firefox-dev -y
@dcagnetta
dcagnetta / BigFileZipper.cs
Last active February 9, 2021 12:44
Big File Zipping C#
// Import: <PackageReference Include="DotNetZip" Version="1.15.0" />
namespace BigFileZipper
{
class Program
{
static void Main(string[] args)
{
string[] filenames = {
@"PUT PATH TO REALLY BIG FILES HERE",
@dcagnetta
dcagnetta / DynamicRepository.cs
Created February 10, 2021 00:38
Dynamically create repositories for entities
var dbEntity = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t =>
t.GetInterface(nameof(IDbEntity)) != null)
.Select(x => Activator.CreateInstance(x) as IDbEntity)
.FirstOrDefault();
var dbRepository = ConstructDatabaseRepository(dbEntity, _dbRepository.DbContext);
// Magic