{"id":18290684,"url":"https://github.com/nikouu/fun-weird-nasty-csharp","last_synced_at":"2025-04-05T10:30:50.296Z","repository":{"id":213642526,"uuid":"734543529","full_name":"nikouu/Fun-Weird-Nasty-CSharp","owner":"nikouu","description":"C# snippets That Make You Go Hmmm...","archived":false,"fork":false,"pushed_at":"2024-09-14T04:54:48.000Z","size":82,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T21:05:46.531Z","etag":null,"topics":["csharp","dotnet"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nikouu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-22T00:49:18.000Z","updated_at":"2024-09-14T08:22:02.000Z","dependencies_parsed_at":"2023-12-22T05:24:08.098Z","dependency_job_id":"e3ad83e9-8267-4add-bddf-2c3f43ccd95d","html_url":"https://github.com/nikouu/Fun-Weird-Nasty-CSharp","commit_stats":null,"previous_names":["nikouu/fun-weird-nasty-csharp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikouu%2FFun-Weird-Nasty-CSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikouu%2FFun-Weird-Nasty-CSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikouu%2FFun-Weird-Nasty-CSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikouu%2FFun-Weird-Nasty-CSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikouu","download_url":"https://codeload.github.com/nikouu/Fun-Weird-Nasty-CSharp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247324415,"owners_count":20920642,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet"],"created_at":"2024-11-05T14:11:52.415Z","updated_at":"2025-04-05T10:30:49.851Z","avatar_url":"https://github.com/nikouu.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fun Weird Nasty C#\nC# snippets That Make You Go Hmmm... \n\nAll snippets are linked to their original creators (or the people I found them from). I don't take any credit, just collating fun bits! Most also have [SharpLab.io](https://sharplab.io/) links so you can see them in action yourself.\n\n## Smallest C#\n\n```csharp\n{}\n```\n\n[Via nietras](https://nietras.com/2021/10/09/worlds-smallest-csharp-program/)\n\n[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxAJwK4wD4G8C+Q===)\n\n## It's all Greek\n\n```csharp\nusing System;\nunsafe class Program\n{\n    delegate void bar(int* i);\n    static Index ƛ(bar β) =\u003e default;\n    static void Main(string[] args)\n    {\n        int[] ω = { };\n        int Ʃ = 42;\n        int? Φ = 10;\n        var ϼ = ω;\n        \n        ϼ=ω[ƛ(β:Δ=\u003eΦ??=ω[^++Ʃ]/Ʃ|*\u0026Δ[-0%Ʃ]\u003e\u003e1^Φ??0!\u0026~(δ:Ʃ,^Φ..).δ)..(1_0\u003e.0?Ʃ:0b1)];\n    }\n}\n```\n[Via Llewellyn Pritchard](https://twitter.com/leppie/status/1492256050774487047)\n\n[SharpLab](https://sharplab.io/#v2:D4AQTAjAsAUArgOwM4EMBmBTABOHEDssA3rFmVgCYYA2GA5igC7YgAsWARigE4AUAlgkYAqLPwCUAblLkQEAGxYAkgioAPLIGzAXl25ZATcDisAXgB8lDGhRxqjaTHJ5FbLAFkUg3nIAMAbQC6WDx0SOIyZCQOjuSCjAFYgJPAJlhEWAC+9tExQliAlYDJrGCZWWJCAPxYgGXAyRDexVkAbjxYgD/AyQn10eHRLcYJvtr6AFyAKcBmlWVlfb4AegDUc7n+APS5wMIAZCO+ALTeAKRLpqYQMxNl3gCEGwB+vIAtwEO5ADRnAHRv4m/3X2+8EAB9bymN7eMq5IbeDgQcT+TppWBpIA)\n\n## Mutate ReadOnly fields\n\n```csharp\nusing System;\nusing System.IO;\n\nConsole.WriteLine(Path.DirectorySeparatorChar); // prints '\\'\n\nvar f = (in char x) =\u003e { /* can't modify 'x' here */ };\nf = (ref char x) =\u003e { x = 'A'; }; // but can here!\n\nf(Path.DirectorySeparatorChar);\n\nConsole.WriteLine(Path.DirectorySeparatorChar); // prints 'A'\n```\n\n[Via Aleksandr Shvedov](https://twitter.com/controlflow/status/1737788743270965538)\n\n[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxAJwK4wD4AEBMBGAWACgsAGAAizwDoBJAeQG5jiqBOACgAUIEALagBEoyAKYBjBAHtkATwDKogA4RkvGQGE+qgJSNyAegPklyWAgDO5AOQAdayyIA3VeQBm5ALzkOscuO1kcgAPHS8APnIAb0MAKn8IGGsEcjApABMoN1kbYOtyPlExcljjAF9mIg9vDjEPANdQiOiQrxsAQWt9CsNjYFQU8USCotEAQkc3bl4BYTFJGQVlVXVkLV1K1jxOHn4hEQlpOUUVNSP15D1ekzMYSw7rIA)\n\n## New DateTime formula, *Euclidean affine functions*\n```csharp\n// Exactly the same as Year, Month, Day properties, except computing all of\n// year/month/day rather than just one of them. Used when all three\n// are needed rather than redoing the computations for each.\n//\n// Implementation based on article https://arxiv.org/pdf/2102.06959.pdf\n//   Cassio Neri, Lorenz Schneiderhttps - Euclidean Affine Functions and Applications to Calendar Algorithms - 2021\ninternal void GetDate(out int year, out int month, out int day)\n{\n    // y400 = number of whole 400-year periods since 3/1/0000\n    // r1 = day number within 400-year period\n    (uint y400, uint r1) = Math.DivRem(((uint)(UTicks / TicksPer6Hours) | 3U) + 1224, DaysPer400Years);\n    ulong u2 = (ulong)Math.BigMul(2939745, (int)r1 | 3);\n    ushort daySinceMarch1 = (ushort)((uint)u2 / 11758980);\n    int n3 = 2141 * daySinceMarch1 + 197913;\n    year = (int)(100 * y400 + (uint)(u2 \u003e\u003e 32));\n    // compute month and day\n    month = (ushort)(n3 \u003e\u003e 16);\n    day = (ushort)n3 / 2141 + 1;\n\n    // rollover December 31\n    if (daySinceMarch1 \u003e= March1BasedDayOfNewYear)\n    {\n        ++year;\n        month -= 12;\n    }\n}\n```\n\n[30% optimization of DateTime.GetDate()/.Year/.Month/.Day/.DayOfYear by 'Euclidean affine functions' #72712](https://github.com/dotnet/runtime/pull/72712)\n\n## ReadOnly lists aren't always readonly\n```csharp\nusing System;\nusing System.Collections.Generic;\n\nvar list = new List\u003cint\u003e{1, 2, 3, 4};\n\nIReadOnlyList\u003cint\u003e readonlyList = list;\n\n// error CS1061: 'IReadOnlyList\u003cint\u003e' does not contain a definition for 'Add'...\n// readonlyList.Add(5);\n\n// Works as expected\n((List\u003cint\u003e)readonlyList).Add(5);\n\n// 5\nConsole.WriteLine(readonlyList.Count);\n```\n\n[Via Nick Chapsas](https://www.youtube.com/watch?v=7hBPI0xYezo\u0026t=203s)\n\n[Great explanation on SO](https://stackoverflow.com/a/49115272)\n\n[SharpLab](https://sharplab.io/#v2:C4LgTgrgdgPgAgJgIwFgBQcAMACOSAsA3OugG4CGY2ANgJYDOw2AvNlAKYDu2AMg8AB5aUYAD4A3kgA02BDIDMM/AF9iadAEkASu3IATAPJRqATz6MhI0djC69Ae2Nn+LGvzXoA9J+zswYeyoAYQBlJEwANiQQbABybTsjU3NBYTFY7Ad2ejZ7JgBjR2ByYWxyTPYAM2FaYFpHbErAuIBBPT1YgDpurx9bfUdk/k62vQAKAFYASg80b2wAdUCAaxzyHPYADwAHdnzgdj10MbGUyzEp/ocnFKmR9smZkjmfCfQ8AE4xq8HnRk6gvZoMAZkA==)\n\n## async async async (async async)\n\n```csharp\nasync async async(async async) =\u003e \n        await async;\n```\n\n[Via Jared Parsons](https://twitter.com/jaredpar/status/1613615815231934468)\n\n[Explainer from Jared](https://gist.github.com/jaredpar/6706628bfe0098840ce453c1c71098a9)\n\n[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxFANnAJiA1AHwAIBMAjALABQeADAAR5EB0ASgKYBmKzAxglAPYz0AomCgIA3OSq0GjAK4weYZvQDCvMAAdUzAE4BlXQDconZgGcJFGnQCsl8gGIYslCgjAO1dFDPuO5SQJaPB0uXh10agBvagBfQOoAdwALXWZouPJqbOoEvQQdWABzaKycvABmaQA2aihNFBNRal4NXQgEcOp8wpgigAp0AE8YCBFOakMIHQBKagBeAD5qZ1dLeLIE4dHxjI3K2iCpnQyAshyvEbGTS52bpZWXFEsL19zz8oAWalDOcPR+ik0t0CsUFo9XHA6gpqENweg2BAXAgZmVslE0a8emCzOCEMkfPRttdOC8crELpjXpi8AAOWjVAA8sAQywAsv05hiPm9jpNwTBmIlOWS3ngAJyTeh0/qCxK0Wmcmai154ADs1EoKo2lJ5Cv1MrpCrmDyp5UldPWZwA9NbqAARXiw3iyagqJLJDrUACSAHIwF5eGDUqEAPwJFQxDZnQi0SUgH0AOV4PFYQzUDWYPH4pT1B2AvF4KB9ZgzGg4CGYkQeBVkzBVBzw3wA4lmWGZkZy9jSqk3qAB5GBlitV/p0IJ/BSwWQdPgwLmZMjRsgAbQAgmYRpw2Vnkrx0AAhWSoBE6foIIZtXisfrAY8oU8zGYAXQSRpiPbj1FbCDXiWglZniayz4jovDyqszzkMusZ3ieujkNyFz5vep5dlGn50LUcEProbqhB0zBdg8coiph3wGAg7adguOrlL2FFZoIAAepgaNmMD9CxbEcdQzC0eRILTAgjIACr5IRbIQJwBKCos/ShKw1DiQgknSbJ6RmKplZSTJsD8Wa2RAqEykSTp6n6dQCbehuW5mcwukaQsIHJGBEFPA2vb0ngtQkR5gl/gBg7DlmVZiYFoi6FCKlqXpcn9IZPxsMpEWAXGUVJUpMXmXFmnaQ5FmCqieoXMZ6SialeHWcmqbpuo5ZZnOiVlaZ+WOZZ1m2TAnD2e1grOdQoHgRCUF5gx1CpQAqjAvisMwwX1SO6Dhf+kU6NFvWFcw8mJYpKWrWlEoZXt2UFbl1BabFGnFW8RkhuVlUnNZKiFDwnAQCgNVQGmIUcc192tVdHU+l1PVtVtA1De5ayCZRm25f0Nmbt18NOZdOXXZDrnDZBVpkEAA==)\n\n## async async async (async async) Part 2\n\n```csharp\n[async, async\u003casync\u003e] async async async([async\u003casync\u003e, async] (async async, async) async)\n        =\u003e await async.async;\n```\n\n[Via Lucas Trzesniewski](https://twitter.com/Lucas_Trz/status/1613628484315713576)\n\n[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxFANnAJiA1AHwAIBMAjALABQeADAAR5EB0ASgK4wJRgCm9AwgPZgADqk4AnAMpiAblADGnAM4BucuQDEMZihQRgKTtXRQFu/aooBmWgWpSIo6gG9qL12/Ju8ADloAWagCyABQAlE4ebq54AJzUMJwA7rb2ofTeQeicAGYQWgghKmRuAL4RLmXUFQDa3nC0XgA83gB8ALr1Hek1jS113u1B3vV9XmHeIRVuALzNtLHeaV6F5qVk5uSEc9Qg1ACSAHJ87FkAnvxC+ux8MOFFUVbAfHwoewrngpec6NQz1AiizE4hU8Vjw/gA4pwEIxFHlQk5qKsQX5qAB5GDvT7oQZEGyya7sTQQK4wMLOVarchVACCChOMFkAShAAs+OgAELMVCZURBBAnQScPhZILALkoHkhEKtDY2IbOCp4UGxSEIakJaAIMTw34IZmiPhJTTaQqUsg0hD/KBirUAVRMAHNOEFqZbRNbmFqACr2J0IBT0anaOpBlCGgJ5KAfAxTP4AzjS2X1V1Wm0GHYp91ppxmi2pz2ce0QJ0ut0e72+qEB0Mh7ThyPRn5xwGJihyryZ8ucBpe2YZsvZxxmzZi7liciORUPcU8+Hkqe0IgANmoo4lYmoPFEnGJzrCv3iCVCwPuKMk0NhKAQc8RC7B1HPAFEAB7yQQkoIvt8k6gJhFI0973EBB7AQHtgN3AIIFkZlYE4Zogm3LJqC9CCtSgmC4OoBQQPQ6DYPiCY7kiagEmZMQDFQ3DOAwgj0z2Wl6VkNCaPwrDdX1Q04i0FATxcJVOhXA8eL42hQX8dVNXRTEoS+HtJKgLVRDqKjILY+IEMmVwkJQhSlLmMQ6h01S8Mw+JsOo2i4KIkiXDIijdI1RSNx2A4jigU4ZJJLS7PI7cUJYqzzNcxiGUC9SY1mPUDSNES7wkpyEFtGATCyThpIEaMtXQeTEsMgLLIizTiMiYy9I3GJ8uM8KzIMHC1NqmzbPs/yvXKhxXK3RS5AgFBDmOM5MsuKBrh80i/Momq6O2Bi6TCwrap+KLONik14ofKEprgoJdlC5iFum+rTLo/dlpi7i1rIVYgA===)\n\n## Floats are inaccurate\n\n```csharp\nusing System;\nfloat x = 0.4f;\nfloat y = 0.6f;\nConsole.WriteLine((double)x + (double)y == 1); // False\nConsole.WriteLine((float)((double)x + (double)y) == 1); // True\n```\n\n[Via Ashley Hauck](https://twitter.com/khyperia/status/1730232117831815322)\n\n[Explainer by Ashley Hauck](https://mas.to/@khyperia/111500086973840518)\n\n[SharpLab](https://sharplab.io/#v2:C4LgTgrgdgPgAgJgIwFgBQAzANgewIbAAEAHoQLyEAMAdACwYDc62+RAnuVdQGyPpxIAnAAphAExwQARlgCmASlIBqQuMkyFHMhSTymaASOEsC80ROlzFhFWsub55HXqA)\n\n## Ensure `finally` with `ThreadAbort`\n\nNote this is for .NET Framework. \n\n```csharp\ntry {}\nfinally\n{\n    if (!m_canceled)\n    {\n        m_canceled = true;\n        TimerQueue.Instance.DeleteTimer(this);\n    }\n}\n```\n\n[Via Roman Marusyk](https://twitter.com/MarusykRoman/status/1644002015431872514)\n\n[Documentation](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-7.0)\n\n## C# lunar cycle performance \n\nAn unsigned number bug in `Environment.TickCount` in a specific situation in a threadpool prevent more threads from being added to the pool.\n\n[Via Kevin Gosse](https://twitter.com/KooKiz/status/1389942946514997248)\n\n[GitHub Issue](https://github.com/dotnet/runtime/issues/51935)\n\n## Dynamic var global using\n\n```csharp\nglobal using var = dynamic;\n```\n\n[Via Jared Parsons via Immo Landwerth](https://twitter.com/terrajobst/status/1785375779179893144)\n\n## Amogus\n\nThis code is C# version (definitely not as nice) of Python code that returns the \"ඞ\" character for the Among Us meme.\n\n```csharp\n// Python code\n// chr(sum(range(ord(min(str(not()))))))\n\n// C# version\nvar amogus = (char)Enumerable.Range(0, (int)true.ToString().Min()).Sum();\n```\n\n[Via @chordbug](https://x.com/chordbug/status/1834642829919781369)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikouu%2Ffun-weird-nasty-csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikouu%2Ffun-weird-nasty-csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikouu%2Ffun-weird-nasty-csharp/lists"}