Skip to content

Instantly share code, notes, and snippets.

@huanlin
huanlin / LineNotifyExample.cs
Created December 1, 2022 14:20
Line Notify Example
namespace ConsoleApp1
{
internal class Program
{
static async Task Main(string[] args)
{
var secretToken = "XYZ"; // <--- Replace with your token.
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", secretToken);
@huanlin
huanlin / maui-create-window.cs
Created November 8, 2022 03:31
MAUI - Create a window
private void Button1_Clicked(object sender, EventArgs e)
{
var win = new Window
{
Page = new NewPage1(),
Width = 1024,
Height = 300 // 亦可在此設定初始座標 X 和 Y
};
Application.Current.OpenWindow(win);
@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/