3 ms·
Not the only good feature, though. I've been doing Advent of Code in C# this year to get my head around it. The standard library and language features seem to b
by gnat 5y ago
Not the only good feature, though. I've been doing Advent of Code in C# this year to get my head around it. The standard library and language features seem to be sane and nicely-designed and I am having fun with it.
As an aside, it's always interesting to see how standard libraries do splits and joins between arrays/tuples/collections and strings.
In Perl they're both functions:
@arr = split(/ /, $str);
$str = join (" ", @arr);
In Python they're methods on the separator or tuple:
arr = str.split(" ")
str = ",".join(tuple)
That idea that you're telling comma to join a group of items always rubbed me wrong. In C#, joining is a class method:
arr = str.Split(" ");
str = string.Join(" ", arr);
Here "string" is a class name. The lack of parallelism grates on me somewhat. I think I would have preferred
str = arr.Join(" ");
Does anyone know why this form is not used?
- arun_m 5y agoThere is a Join method in Linq, with entirely different meaning and matches string[] https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.join?view=net-6.0 https://docs.microsoft.com/en-us/dotnet/api/system.linq.enum...
- deleted 5y ago[deleted]
- to11mtm 5y ago> Does anyone know why this form is not used? Likely because early versions of C# (Pre-generics) wouldn't have had a good way to specifically offer a .Join method on string arrays specifically. Now that we have generics, .Join() on an array normally refers to a type of Linq operation where you join two sequences together. You actually probably could make an extension method for your own usage in-app that would give you this functionality; if I had to guess why they didn't, I would assume it would consume Linq users. OTOH that's kinda the nice thing about C# though. It's easy to go: public static class StringArrayExtensions { public static string Join(this string[] arr, string joinToken) { return string.Join(joinToken, arr); } } and package such utility functions into a shared lib for your team to use.