SERKUT YILDIRIM’s Post

💡 𝐂#/.𝐍𝐄𝐓 𝐓𝐢𝐩 - 𝗦𝘁𝗿𝗶𝗻𝗴 𝐔𝐬𝐚𝐠𝐞 🔥 💎 𝗧𝗵𝗿𝗲𝗲 𝗪𝗮𝘆𝘀 𝗳𝗼𝗿 𝗰𝗵𝗲𝗰𝗸𝗶𝗻𝗴 𝘀𝘁𝗿𝗶𝗻𝗴𝘀 👍 𝗨𝘀𝗶𝗻𝗴 𝗦𝘁𝗮𝗿𝘁𝘀𝗪𝗶𝘁𝗵 𝗮𝗻𝗱 𝗘𝗻𝗱𝘀𝗪𝗶𝘁𝗵 𝗺𝗲𝘁𝗵𝗼𝗱𝘀 The most readable and explicit approach provided by the String class. These methods clearly express your intent and make your code self-documenting when checking string boundaries. 💡 𝗨𝘀𝗶𝗻𝗴 𝗜𝗻𝗱𝗲𝘅𝗲𝗿 𝗣𝗿𝗼𝗽𝗲𝗿𝘁𝘆 𝗮𝗻𝗱 ^ 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿 Direct character access using indexer syntax. The ^ operator (index from end) introduced in C# 8.0 provides a concise way to check the last character without calculating string length. 🔥 𝗨𝘀𝗶𝗻𝗴 𝗟𝗶𝘀𝘁 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 The modern List Pattern feature introduced in C# 11. This pattern matching approach treats strings as character sequences, where .. (Slice Pattern) represents any elements in between, creating expressive and elegant validation code. 🤔 Which one do you prefer? #csharp #dotnet #programming #softwareengineering #softwaredevelopment

  • No alternative text description for this image

Well put! I tend to favor StartsWith and EndsWith for clearer intent, though I have to admit that list patterns bring a fresh, contemporary touch to the code’s style.

Like
Reply

Very nice. I'd go with option 1 - Start/End with, and add a null reference check to it. Even though option 3 - List Pattern includes the null reference check, readability is more important. How many times have you looked at a piece of code and did not understand what the programmer wanted to accomplish? Sometimes the programmer was you, and you were revisiting code you wrote a few months earlier. I'm old school and would have added brackets as well for readability, but that's me 😊

Like
Reply

Great post, Serkut! From my experience, using StartsWith/EndsWith is best for readability and quick checks, while list patterns shine in cleaner, modern C# syntax for complex matching

Great tips! I'm a big fan of StartsWith/EndsWith for readability, but the new list patterns are seriously tempting.

Nice roundup, I’d prefer StartsWith and EndsWith for clarity, but list patterns definitely make the code feel modern and expressive.

Nice Share but please note that using Span<T> can also make a big difference. especially for performance-critical string operations where you want to avoid unnecessary allocations.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories