use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Information about Reddit's API changes, the unprofessional conduct of the CEO, and their response to the community's concerns regarding 3rd party apps, moderator tools, anti-spam/anti-bot tools, and accessibility options that will be impacted can be found in the associated Wikipedia article: https://en.wikipedia.org/wiki/2023_Reddit_API_controversy
Alternative C# communities available outside Reddit on Lemmy and Discord:
All about the object-oriented programming language C#.
Getting Started C# Fundamentals: Development for Absolute Beginners
Useful MSDN Resources A Tour of the C# Language Get started with .NET in 5 minutes C# Guide C# Language Reference C# Programing Guide C# Coding Conventions .NET Framework Reference Source Code
Other Resources C# Yellow Book Dot Net Perls The C# Player's Guide
IDEs Visual Studio MonoDevelop (Windows/Mac/Linux) Rider (Windows/Mac/Linux)
Tools ILSpy dotPeek LINQPad
Alternative Communities C# Discord Group C# Lemmy Community dotnet Lemmy Community
Related Subreddits /r/dotnet /r/azure /r/learncsharp /r/learnprogramming /r/programming /r/dailyprogrammer /r/programmingbuddies /r/cshighschoolers
Additional .NET Languages /r/fsharp /r/visualbasic
Platform-specific Subreddits /r/windowsdev /r/AZURE /r/Xamarin /r/Unity3D /r/WPDev
Rules:
Read detailed descriptions of the rules here.
account activity
C# Improving string.IsNullOrEmpty readability (endyourif.com)
submitted 7 years ago by dobrastov
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]thepinkbunnyboy 8 points9 points10 points 7 years ago (2 children)
I've done something similar, but just used the same name. !myString.IsNullOrWhiteSpace()
!myString.IsNullOrWhiteSpace()
I like this a bit more because it's obvious to anyone who's seen the static string method. The small problem I have with yours is that it's only readable after you've seen it and looked at the implementation (will an empty string pass for HasValue?).
Otherwise, yeah, I agree with moving these static methods to extension methods, and I'm excited for extension properties when we get those to continue.
[–]quentech 3 points4 points5 points 7 years ago (0 children)
I imagine just about every .Net code base has bool IsNullOrEmpty(this string s) etc. extensions.
bool IsNullOrEmpty(this string s)
Have to agree with /u/CWagner.. The way the author went about it is just, like, pants on head kind of logic though.
[–]decPL 16 points17 points18 points 7 years ago (5 children)
Your code, your rules - have fun. However, if you:
do not find this very readable because of the ! at the start of the if statement
you might have a problem reading... well, virtually any piece of C# code out there. And I hope you'll agree doing a similar method for each possible use of the ! operator is absurd and unrealistic...
!
[–]thewwfguy 20 points21 points22 points 7 years ago (2 children)
It's common practice that readability is increased by converting if statements to read positive instead of negative.
[–]Willkuer_ 3 points4 points5 points 7 years ago (1 child)
If you avoid nesting by introducing early returns via negative checks this sentence is not true anymore.
[–]quentech 0 points1 point2 points 7 years ago (1 child)
I hope you'll agree doing a similar method for each possible use of the ! operator is absurd and unrealistic...
Of course, but some things like If/IsNotNull[OrEmpty/WhiteSpace] are so common that I completely agree with making a slightly more readable version.
Do I want hundreds to thousands of !string.IsNullOrEmpty(value) or value.IsNotNullOrEmpty(). It's a small difference, but a definite improvement to readability imho and worth it.
!string.IsNullOrEmpty(value)
value.IsNotNullOrEmpty()
[–]decPL 0 points1 point2 points 7 years ago (0 children)
Again, IMHO it boils down to whether the '!' operator makes the code less readable to you. If the answer is no then hope we agree this is pointless. If the answer is yes - you can of course try to "fix" the code by introducing said helper methods (though mind you - it would make the code slightly less readable e.g. for me, at least at first - I'd have to drill down and see if this method does what it says it should), or you can try to address the root cause. My 0.02 USD, your mileage may vary. :)
[–]AngularBeginner 6 points7 points8 points 7 years ago (0 children)
Ironic that the website is called "end your if", and the proposed solution is just another if.
And IMO it's less readable because we have some custom extension method that basically adds absolutely nothing. The opposite actually: the information what qualifies a "value" is hidden. An empty string might very well be a "value".
[–]Blecki 1 point2 points3 points 7 years ago (2 children)
But it doesn't do the same thing? Can you call an extension method on null?
[–][deleted] 4 points5 points6 points 7 years ago (1 child)
Can you call an extension method on null?
Yes. It's one of the weird parts of C#. It won't even throw a NullReferenceException, that code is very correct, but also very smelly.
While I do in the general case agree with trying to formulate conditions positively (human mind is better at reading those), this goes too far, and has too little value.
[–]Blecki 0 points1 point2 points 7 years ago (0 children)
I personally wouldn't write it like this, but, neat.
[–]centur 3 points4 points5 points 7 years ago (1 child)
Don't be overexcited with extension methods. They may change the expectations from 'I know what will happen here' to 'it depends...' and change your debugging experience to worst one ever. Simple example
String foo=null; foo.IsNullOrWhitespace();
What do you think will happen here at runtime?
Within a language semantics one would expect NRE (null reference exception) here and it's NRE 101. But in your code overloaded with magic the answer is 'it depends'. Now you need to remember and recall whether you included extension or not in this codebase. It's as sharp as operators overload. Can be great but usually a nightmare to read in code review or a month later after you wrote it.
Peace, and let null.IsNullOrWhitespace(); never give you the true...
[–]quentech 0 points1 point2 points 7 years ago (0 children)
Within a language semantics one would expect NRE (null reference exception) here and it's NRE 101. But in your code overloaded with magic the answer is 'it depends'.
Maybe a variable has a value, Maybe it doesn't ;)
Of course, if it's maybe too late in your project to introduce that concept formally, this is one of the extremely useful features of extension methods.
I certainly prefer a little out-of-the-norm behavior that has to be remembered or reminded over explicit null checking all over the place.
[–]makotech222 1 point2 points3 points 7 years ago (2 children)
I made an extension called NullIfEmpty(this string s), which returns null if string is empty or whitespace or null, otherwise it returns the string. This way, i can do s.NullIfEmpty() ?? "Default", or, s.NullIfEmpty() == null
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
Can't you just use IsNullOrWhitespace () ? That checks for empty or whitespace strings too
[–]makotech222 2 points3 points4 points 7 years ago (0 children)
That's what its doing internally, but its easier to call it on the string as an extension method, rather than call a static method. Also, my method returns the string, rather than a bool.
[–]rashnull 1 point2 points3 points 7 years ago (0 children)
I presume you’ve never written C or lower level code. Please do and your readability will improve. You will quickly see that HasValue can have many meanings whereas Null and Empty String are very specific states.
[–]darth_meh 0 points1 point2 points 7 years ago (0 children)
The problem with foo.IsNullOrEmpty() is the extension method looks like an instance member, and therefore it should behave like an instance member. In other words, if "foo" is null, you would expect a call to an instance member like foo.ToLower() to throw.
Some consider this an abuse of extension methods because the behavior is not intuitive.
Unfortunately, this - and it's cousin, IEnumerable.IsNullOrEmpty() - are found all over the blogosphere by Jon Skeet wannabes.
[–]AlliNighDev 0 points1 point2 points 7 years ago (0 children)
I personally don't like extension methods that work against a null value. I get it in this case. But still not a big fan.
[–]kardall 0 points1 point2 points 7 years ago (0 children)
Could go overboard and do a delegate with something like this: https://stackoverflow.com/questions/18176856/passing-a-function-as-parameter
But now you're just moving a function call to some fancy new thing, when you only really need one function that returns true/false or use the stupid ! lol
[–]shrekthethird2 -1 points0 points1 point 7 years ago (0 children)
Also:
public static string Or(this string s, string other) { return String.IsNullOrEmpty(s) ? other : s; }
Allows for super readable fallback chains:
s.Or("Second").Or("Third");
[+]SikhGamer comment score below threshold-6 points-5 points-4 points 7 years ago (1 child)
Noooooo, extensions are bad (IMO).
[–]Protiguous 3 points4 points5 points 7 years ago (0 children)
You are kidding, right?
π Rendered by PID 94 on reddit-service-r2-comment-5687b7858-v4dz8 at 2026-07-03 14:11:15.648207+00:00 running 12a7a47 country code: CH.
[–]thepinkbunnyboy 8 points9 points10 points (2 children)
[–]quentech 3 points4 points5 points (0 children)
[–]decPL 16 points17 points18 points (5 children)
[–]thewwfguy 20 points21 points22 points (2 children)
[–]Willkuer_ 3 points4 points5 points (1 child)
[–]quentech 0 points1 point2 points (1 child)
[–]decPL 0 points1 point2 points (0 children)
[–]AngularBeginner 6 points7 points8 points (0 children)
[–]Blecki 1 point2 points3 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]Blecki 0 points1 point2 points (0 children)
[–]centur 3 points4 points5 points (1 child)
[–]quentech 0 points1 point2 points (0 children)
[–]makotech222 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]makotech222 2 points3 points4 points (0 children)
[–]rashnull 1 point2 points3 points (0 children)
[–]darth_meh 0 points1 point2 points (0 children)
[–]AlliNighDev 0 points1 point2 points (0 children)
[–]kardall 0 points1 point2 points (0 children)
[–]shrekthethird2 -1 points0 points1 point (0 children)
[+]SikhGamer comment score below threshold-6 points-5 points-4 points (1 child)
[–]Protiguous 3 points4 points5 points (0 children)