C# extension method just got easier

We used to write code like this for setting up our extension. 

public static class MyExtensions { public static int WordCount(this string str) => str.Split([' ', '.', '?'], StringSplitOptions.RemoveEmptyEntries).Length; }

The new way of writing extension code is here


public static class MyExtensions { extension(string str) { public int WordCount() => str.Split([' ', '.', '?'], StringSplitOptions.RemoveEmptyEntries).Length; } }

Magic! 

Comments

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type