Skip to content

Instantly share code, notes, and snippets.

View karenpayneoregon's full-sized avatar
🎯
Focusing

Karen Payne karenpayneoregon

🎯
Focusing
View GitHub Profile
@karenpayneoregon
karenpayneoregon / Customer1.cs
Created June 6, 2024 11:15
Provides three different way to provide change notification
public class Customer : INotifyPropertyChanged
{
private string _firstName;
private string _lastName;
private DateOnly _birthDate;
public string FirstName
{
get => _firstName;
set
@karenpayneoregon
karenpayneoregon / CommonHelpers.cs
Created June 3, 2024 12:50
Get season Northern hemisphere
public static class CommonHelpers
{
public static string GetSeason(int month) => $"The season is {month switch
{
1 or 2 or 12 => "winter",
> 2 and < 6 => "spring",
> 5 and < 9 => "summer",
> 8 and < 12 => "autumn",
_ => "unknown.",
}}.";
@karenpayneoregon
karenpayneoregon / Example.cs
Created May 31, 2024 11:51
Foreach with indexer
var paths = Environment.GetEnvironmentVariable("Path")!.Split(";");
foreach (var (part, index) in paths.Select((part, index) => (value: part, i: index)))
{
Console.WriteLine($"{index,-3}{part}");
}
string firstLastName = "KarenPayne";
var separated = firstLastName.SplitByCase();
@karenpayneoregon
karenpayneoregon / Demo.cs
Created May 21, 2024 12:43
Case insensitive string key dictionary
static TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
static List<string> names = ["KaRen", "YelEna", "BiLL"];
private static void CaseInsensitiveDictionary1()
{
Dictionary<string, string> userDictionary = new(StringComparer.InvariantCultureIgnoreCase)
{ { "karen", "Developer advocate" }, { "yelena", "Manager" }, { "bill", "Web master" }, };
foreach (var name in names)
@karenpayneoregon
karenpayneoregon / StringExtensions.cs
Created May 17, 2024 13:49
Lower case first character in a string
public static class StringExtensions
{
public static string? FirstCharacterToLowerCase(this string? sender)
{
if (!string.IsNullOrEmpty(sender) && char.IsUpper(sender[0]))
{
return sender.Length == 1 ?
char.ToLower(sender[0]).ToString() :
$"{char.ToLower(sender[0])}{sender[1..]}";
}
@karenpayneoregon
karenpayneoregon / Program.cs
Created May 14, 2024 13:49
Get numbers values from string
using System.Globalization;
using System.Text.RegularExpressions;
namespace ConsoleApp1;
internal partial class Program
{
static void Main(string[] args)
{
var input = "Enter anything 12 and I will try 44 to detect any number (leave empty 1 to exit):";
@karenpayneoregon
karenpayneoregon / Demo.cs
Created May 13, 2024 15:33
Extract multiple numbers from a string (C#)
string input = "There are 5 numbers in this string: 40, 30, and 10. 44";
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
if (string.IsNullOrEmpty(value)) continue;
if (int.TryParse(value, out var number))
{
Debug.WriteLine($"Number: {number,-3:D2}");
}
}
@karenpayneoregon
karenpayneoregon / TestContainer.cs
Created May 11, 2024 10:46
Experimenting with SQL
public class TestContainer
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
@karenpayneoregon
karenpayneoregon / ListDictionary.cs
Created May 4, 2024 11:47
Provides a method to add items to a dictionary with a list of string for value
public class ListDictionary
{
private Dictionary<string, List<string>> _internalDictionary = new();
public Dictionary<string, List<string>> Dictionary => _internalDictionary;
public bool HasItems => _internalDictionary.Count > 0;
public void Add(string key, string value)
{
if (_internalDictionary.TryGetValue(key, out var item))
{