19
top 4 comments
sorted by: hot top controversial new old
[-] Rogue@feddit.uk 4 points 1 month ago

string.IsNullOrEmpty(myStr) has always annoyed me, why isn't it an extension method or a non-static method?

myStr.IsNullOrEmpty() just feels cleaner and more intuitive to me.

[-] RonSijm@programming.dev 4 points 1 month ago

myStr.IsNullOrEmpty() feels a bit weird to me, because you have to know that it's an extension method.

Otherwise it kinda looks like you might be trying to run a method of something that's possibly null

That's the same design principle of why ArgumentNullException.ThrowIfNull(myStr) is not an extension method

[-] JakenVeina@lemm.ee 3 points 1 month ago

The usual convention is to have extension methods behave like regular methods, I.E. don't allow them to accept nulls. When looking at foo.Bar(); it's better if I can just intuit that foo can't be null because you're not supposed to be able to invoke the . operator on null.

Of course, I'm entirely guilty of breaking this rule myself. cough Shoudly cough FluentAssertions cough

[-] Rogue@feddit.uk 2 points 1 month ago* (last edited 1 month ago)

I've always used the Nullable context so typically I'm just using string.IsNullOrEmpty to determine empty strings, I'm already confident null isn't leaking. But your explanation does make sense.

I'm now wondering why I've never just used myStr != ""

this post was submitted on 14 May 2024
19 points (100.0% liked)

C Sharp

1462 readers
6 users here now

A community about the C# programming language

Getting started

Useful resources

IDEs and code editors

Tools

Rules

Related communities

founded 1 year ago
MODERATORS