Skip to content

Instantly share code, notes, and snippets.

View AnthonyGiretti's full-sized avatar
💭
👍

[MVP] Anthony Giretti AnthonyGiretti

💭
👍
View GitHub Profile
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 6, 2025 22:20
Before and with C# 13 escape sequence
// Before C# 13
Console.WriteLine("\u001b[31mThis is red text\u001b[0m");
// With C# 13
Console.WriteLine("\e[31mThis is red text\e[0m");
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 6, 2025 22:08
C# 13 EnterScope usage example
using System;
using System.Threading;
class LockingDemo
{
private static System.Threading.Lock lock = new();
static void Main()
{
PerformSafeOperation();
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 6, 2025 22:01
Locking with C# 13
private System.Threading.Lock _lock = new();
void Update()
{
lock (_lock)
{
// safely synchronized
}
}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 6, 2025 21:55
Locking before C# 13
private readonly object _lock = new();
void Update()
{
lock (_lock)
{
// critical section
}
}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active April 6, 2025 21:53
C# 13 custom params that implements IEnumerable<T>
void Log(params MyCustomLogCollection logs)
{
foreach (var log in logs)
Console.WriteLine(log);
}
public class MyCustomLogCollection : IEnumerable<string>
{
private readonly List<string> _logs = new();
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 6, 2025 21:42
C# 13 params
void Print(params Span<int> values)
{
foreach (var value in values)
Console.WriteLine(value);
}
Span<int> span = stackalloc int[] { 10, 20, 30 };
Print(span);
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active April 6, 2025 21:36
C# 12 and before params
void PrintNumbers(params int[] numbers)
{
foreach (var number in numbers)
Console.WriteLine(number);
}
var myList = new List<int> { 1, 2, 3 };
PrintNumbers(myList.ToArray());
@AnthonyGiretti
AnthonyGiretti / FlowCleanupTimeTrigger.cs
Created December 10, 2024 15:26
Example of .NET 6 in-process DurableClient
[FunctionName("FlowCleanupOrchestratorTimeTrigger")]
public async Task Run([TimerTrigger("%FlowCleanupTimeTrigger%", RunOnStartup = true)] TimerInfo myTimer,
[DurableClient] IDurableOrchestrationClient client)
{
var instanceId = Guid.NewGuid().ToString();
try
{
_logger.LogInformation($"FlowCleanupOrchestratorTimeTrigger Timer trigger function activated for instanceId: {instanceId}");
@AnthonyGiretti
AnthonyGiretti / FlowCleanupOrchestrator.cs
Created December 10, 2024 15:20
Example if a DurableClient on .NET 8+ isolated
[Function("FlowCleanupOrchestratorTimeTrigger")]
public async Task Run([TimerTrigger("%FlowCleanupTimeTrigger%", RunOnStartup = true)] TimerInfo myTimer,
[DurableClient] DurableTaskClient client)
{
var instanceId = Guid.NewGuid().ToString();
try
{
_logger.LogInformation($"FlowCleanupOrchestratorTimeTrigger Timer trigger function activated for instanceId: {instanceId}");
@AnthonyGiretti
AnthonyGiretti / ExportOrchestrator.cs
Last active December 10, 2024 15:41
Example of an OrchestrationTrigger with .NET 8 isolated, Voluntarily missing some business logic)
[Function(nameof(ExportOrchestrator))]
public async Task<string> RunOrchestrator([OrchestrationTrigger] TaskOrchestrationContext context)
{
try
{
// Handling business logic
// ...
var entityId = new EntityInstanceId(nameof(ExportProcessState), entityKey);