Skip to content

Instantly share code, notes, and snippets.

@huanlin
huanlin / maui-change-window-size-position.cs
Last active October 28, 2022 17:54
Change size and position when a window is activated. For MAUI on Windows platform.
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
protected override Window CreateWindow(IActivationState activationState)
@huanlin
huanlin / random-thread-safe-issue.cs
Last active October 11, 2022 05:02
觀察 Random API 的執行緒安全問題
using System;
using System.Linq;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
var rng = new Random();
Parallel.For(0, 16, _ =>
@huanlin
huanlin / for-forfiles.cmd
Last active July 9, 2022 07:56
Multiple Masks with forfiles and for commands
REM 把需要備份的檔案名稱全丟到 filelist.txt 裡面
del filelist.txt
for %m in (.txt, .pdf, .xls, .xlsx, .xlsm, .doc, .docx, .docm)
do (forfiles /S /M *%m /D +2021/01/01 /C "cmd /c echo @path" >> filelist.txt)
using System.Runtime.CompilerServices;
......
[InterpolatedStringHandler]
public ref struct MyLoggerInterpolatedStringHandler
{
private DefaultInterpolatedStringHandler _handler;
public MyLoggerInterpolatedStringHandler(
int literalLength, int formattedCount,
MyLogger logger, out bool handlerIsValid)
@huanlin
huanlin / csharp10-string-interpol-perf-1.cs
Created March 21, 2022 04:02
C# 10 String Interpolation Performance
public string Today()
{
var d = DateTime.Now;
return $"今天是 {d.Year} 年 {d.Month} 月 {d.Day} 日";
}
@huanlin
huanlin / csharp-record-compiled.cs
Created March 1, 2022 11:49
C# Compiled Record Type
class Student : IEquatable<Student>
{
public int Id { get; init; }
public string Name { get; init; }
public Student(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
@huanlin
huanlin / AsyncEnumerator.cs
Last active February 22, 2022 11:17
C# Async Streams
IAsyncEnumerator<int> e = GetNumbers().GetAsyncEnumerator();
while (await e.MoveNextAsync())
{
Console.WriteLine($"取得 {e.Current}");
}
@huanlin
huanlin / SourceGrid-AppVeyor-sample1.yml
Created January 31, 2022 08:50
SourceGrid AppVeyor.yml Sample
skip_non_tags: true
deploy:
- provider: NuGet
api_key:
secure: F+FEV17LBxIXlSSCC5LD2P9rhkpjGzzD34LaalIJRCSW+8mM9YeI08i9WU/0ZojP
on:
APPVEYOR_REPO_TAG: true
artifact: /.*(\.|\.s)nupkg/
@huanlin
huanlin / FlubuCore_TestAsyncTargets.cs
Created April 16, 2020 11:49
More build scripts for testing
public class DefaultTestScript : DefaultBuildScript
{
protected override void ConfigureTargets(ITaskContext context)
{
var doExample = context.CreateTarget("DoExample")
.Do(t => { throw new Exception("error on purpose."); });
var doExample2 = context.CreateTarget("DoExample2")
.Do(t => { Console.WriteLine("from doExample2"); });
var test = context.CreateTarget("test")
.Do(t => { Console.WriteLine("from test"); });
@huanlin
huanlin / AfterAspNetCore3.cs
Last active April 17, 2020 11:04
Mirgating from IHostingEnvironment to IHostEnvironment for ASP.NET Core 2.x and 3.x
// Note: this line is deleted: using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
#if NETCOREAPP3_1
using Microsoft.Extensions.Hosting;
#else
using IHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
namespace MyApp.WebApi.Controllers
{