Skip to content

Instantly share code, notes, and snippets.

using System;
namespace TextBasedAdventure
{
public class Enemy
{
public Enemy(string name, int power, int hp)
{
Name = name;
Power = power;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
class Program
{
static void Main(string[] args)
{
@binarycow
binarycow / Game.cs
Created March 24, 2021 02:08
Game with screen manager
public class Game1 : Game
{
private readonly ScreenManager _screenManager;
public Game1()
{
_ = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
_screenManager = new ScreenManager();
public sealed class RelativeFontSizeHelper
{
public static readonly DependencyProperty RelativeFontSizeProperty = DependencyProperty.RegisterAttached(
"RelativeFontSize",
typeof(double),
typeof(RelativeFontSizeHelper),
new ((double)0, RelativeFontSizeChanged)
);
public static double GetRelativeFontSize(TextBlock textBlock)
@binarycow
binarycow / MonogameTest.fsproj
Created January 7, 2022 19:52
F# Monogame sample
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
<Nullable>enable</Nullable>
<WarnOn>3390;$(WarnOn)</WarnOn>
<LangVersion>preview</LangVersion>
@binarycow
binarycow / CustomCommand.cs
Created June 25, 2022 17:18
Custom RelayCommand<T> that accepts null values for value types
using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input; // https://www.nuget.org/packages/CommunityToolkit.Mvvm
public class CustomCommand<T> : IRelayCommand<T>
{
private readonly Action<T?> execute;
private readonly Predicate<T?>? canExecute;
public CustomCommand(Action<T?> execute, Predicate<T?>? canExecute = null)
@binarycow
binarycow / CustomButton.cs
Created June 25, 2022 17:26
Custom Button that handles CommandParameter CanExecute issue
using System.Windows;
using System.Windows.Controls;
/// <remarks>
/// Handles issue seen here:
/// https://stackoverflow.com/a/24669638/29249
/// </remarks>
public class CustomButton : Button
{
static CustomButton()
@binarycow
binarycow / BitVector.cs
Last active June 26, 2022 17:56
Generic BitVector
using System.Diagnostics.CodeAnalysis;
public struct BitVector<T>
: IBitVector<BitVector<T>, T>,
IEquatable<T>, IEqualityOperators<BitVector<T>, T>
where T : IEquatable<T>,
IBitwiseOperators<T, T, T>,
IShiftOperators<T, T>,
IMinMaxValue<T>,
IBinaryInteger<T>,
@binarycow
binarycow / naiveparse.cs
Created July 19, 2022 15:53
PhoneNumber Example
public static PhoneNumber Parse(string text)
{
var match = Regex.Match(text, @"^\(((?<areaCode>\d{3})\)\s*)?(?<exchange>\d{3})-(?<number>\d{4})$");
var areaCode = string.IsNullOrWhiteSpace(match.Groups["areaCode"].Value)
? null
: (int?)int.Parse(match.Groups["areaCode"].Value);
var exchange = int.Parse(match.Groups["exchange"].Value);
var number = int.Parse(match.Groups["number"].Value);
if (areaCode is < 0 or > 999)
throw new Exception("Area code is invalid");
public readonly struct OneVarInterpolationPoint
: IComparable<OneVarInterpolationPoint>,
IComparable<decimal>,
IComparable,
IEquatable<OneVarInterpolationPoint>,
IEquatable<decimal>
{
public OneVarInterpolationPoint(decimal var, decimal result)
{