Coverage Topic
- string.empty vs. double quotes (“”)
- string.Equals vs. ==
- string.Intern
- Strings vs. StringBuilder
- When to choose StringBuilder
- String concatenation vs. string.Format
- Advantages of string.Format
- Case insensitive comparisons
- Out vs. return
- Remove un-necessary Blank Lines
- Linq - First vs. FirstOrDefault
- When to use FirstOrDefault
- Single vs. SingleOrDefault
- Performance: FirstOrDefault vs. SingleOrDefault
- IDisposable.Dispose vs. Using Block
Let's Drill Down the Basic Concept
It's best to follow some guidelines because:
- It will remove extra headaches.
- It will improve code readability.
- Better communication programmer to programmer.
string.empty vs. double quotes (“”)
- string.empty improves readabilty than double quotes (“”).

string.Equals vs. ==
string.Equals and “==” are same for primitive data types or value types (int, double, char, bool);Because it is content base comparison.
- “==” compares object references; calls at compile time;
- ‘string.Equals’ compares values; calls at runtime;
string.Equals improves readabilty than ==
string.Intern
If we need to copy the same value of the string again and again, then better to use string.Intern to improve the performance.
Strings vs. StringBuilder
- The term immutable means: The data value may not be changed.
The values of the variable may be changed, but the original immutable values will be discarded and a new data value will be created in memory.
- Strings are immutable and StringBuilder is mutable.
- If you need large amount of string manipulation, use StringBuilder.
- Strings are immutable. So, if you change your strings, then every time a new instance will be created into the memory.
When to choose StringBuilder
If you need more than THREE times string manipulating, use StringBuilder.
String concatenation vs. string.Format

- Do use AppendFormat for StringBuilder.
- Do use String.format for string.
Advantages
- Improve readability.
- Improve performance.
- Easy to modify.
Case insensitive comparisons
Out vs. return
The method that use return is faster than out.
Remove Un-necessary Blank Lines
Better to remove unnecessary blank lines and white spaces.
- It improves code readability.
Linq - First vs. FirstOrDefault
First
There is one or more than one element in the result, but you need the first one and empty is not okay. It throws an exception, if list is empty;
FirstOrDefault
There is one or more than one element in the result, but you need first one or empty is okay. It throws an exception, if source-list is null.
When to use FirstOrDefault
- When more than one results are expected and you need the first record.
- Or if there is no result; but you want default value.
- No exception, if result is empty.
Single vs. SingleOrDefault
- single - If you need only 1 element(not more than 1 or zero).
- SingleOrDefault - If you need only 1 or zero element(not more than 1).
Performance - FirstOrDefault vs. SingleOrDefault
FirstOrDefault is faster than SingleOrDefault.
IDisposable.Dispose vs. Using Block
Better to use 'Using block'. It calls dispose, although there could have an exception or return statement.
Magic Value
Don't use magic value (hard coded string, number) directly in your code block. Place all of the hard coded values into one separate class or at least on top of the class. It will improve code manageability.
Still I want to say more but I'm used to caffeine-driven development. I have already finished my coffee. So, no more words. If you maintain good practices, you are ready to move on to the next journey. Don't let yourself down.
Jeffrey BuppPosted Aug 25, 2020, 10:26 PM
You should add "stop using if blocks to set boolean values" and "stop using one-off variables".
Prasham SabadraPosted Jul 11, 2017, 11:17 PM
Thanks for Sharing! Really helpful.
Sujeet SumanPosted Jul 5, 2017, 4:01 AM
Really this is excellent.
Trung DuongPosted Jul 3, 2017, 9:32 PM
Thanks man, it's very useful for me
Shovan SahaPosted Jun 19, 2017, 12:49 PM
Good findings with sharp sight. These are helpful for best practice. Thanks.
Vivek KumarPosted Jun 16, 2017, 9:54 AM
Worth to read it...
Marco Dalla LiberaPosted Jun 16, 2017, 4:15 AM
Thank u! Very useful,.
Sumit DeshmukhPosted Jun 15, 2017, 5:38 AM
Useful.Thanks for sharing
Prasanna KarunarathnaPosted Jun 14, 2017, 8:50 PM
Very valuable. Thank u
Saravanakumar SekaranPosted Jun 14, 2017, 11:24 AM
Wow very nice article !
Prabu ElavarasanPosted Jun 14, 2017, 9:16 AM
Great.. Article. Please share similar stuffs.
Ravi PatelPosted Jun 14, 2017, 1:44 AM
Very useful thanks for share ....
Manav PandyaPosted Jun 14, 2017, 12:00 AM
Thanks for share .............