Skip to content

Instantly share code, notes, and snippets.

View davidelettieri's full-sized avatar

Davide Lettieri davidelettieri

View GitHub Profile
public class Result {
public bool Success {get;}
public string Message {get;}
public Result(bool success, string message) {
Success = success;
Message = message;
}
}
bool ValidateEmail(Customer c) { /**/ }
bool ValidateNameSurname(Customer c) { /**/ }
bool SendEmail(Customer c) { /**/ }
bool SendEmailToCustomerIfValid(Customer c) {
if(!ValidateNameSurname(c))
return false;
if(!ValidateEmail(c))
return false;
public class Order
{
private List<OrderItem> _items;
public List<OrderItem> GetItems() => new List<OrderItem>(_items);
public void Add(OrderItem item)
{
_items.Add(item);
}
public class Order
{
public bool IsPaid { get; set; }
private List<OrderItem> _items;
public List<OrderItem> GetItems() => new List<OrderItem>(_items);
public void Add(OrderItem item)
{
if (IsPaid)
Union<int, None> a = new Union<int, None>(1);
Union<int, None> b = new Union<int, None>(new None());
// Here we are saying:
// if a (or b) contains an int, add 1 to the value and return the result
// if a (or b) contains None, just return 0 as a default value
var c = a.Match(v => v + 1, _ => 0); // this is 2
var d = b.Match(v => v + 1, _ => 0); // this is 0
public class Union<TOne, TOther>
{
private readonly TOne _toneValue;
private readonly TOther _totherValue;
private readonly UnionType _type;
public Union(TOne value)
{
_toneValue = value;
_type = UnionType.TOne;
int? a = 1;
int? b = null;
bool ba = a.HasValue; // true
bool bb = b.HasValue; // false
int c = b.Value + 1; // throws
@davidelettieri
davidelettieri / Union.cs
Last active March 25, 2020 21:57
Base union type
public class Union<TOne, TOther>
{
private readonly TOne _toneValue;
private readonly TOther _totherValue;
public Union(TOne value) => _toneValue = value;
public Union(TOther value) => _totherValue = value;
}
@davidelettieri
davidelettieri / Program.cs
Created February 4, 2020 11:51
Custom serializer ignored when deserializing arrays
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace TestEnumSerialization
{
class Program
{
static void Main(string[] args)
{
@davidelettieri
davidelettieri / Foo.cs
Created October 12, 2019 16:16
FooNoBoxing
public class Foo
{
public int Value { get; set; }
public override string ToString()
{
return $"{{{nameof(Value)}={Value.ToString()}}}";
}
}