Coder Baba
2.42K subscribers
1.01K photos
23 videos
722 files
723 links
Everything about programming for beginners.
1 and only official telegram channel of CODERBABA India.

Content:
.NET Developer,
Programming (ASP. NET, VB. NET, C#, SQL Server),
& Projects
follow me https://linktr.ee/coderbaba
*Programming
*Coding
*Note
Download Telegram
โœ”๏ธ ๐—”๐˜ƒ๐—ผ๐—ถ๐—ฑ ๐—ฒ๐—น๐˜€๐—ฒ ๐—ฎ๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป
โœ”๏ธ ๐—”๐˜ƒ๐—ผ๐—ถ๐—ฑ ๐—ฒ๐—น๐˜€๐—ฒ ๐—ฎ๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป

โŒ When a return statement is encountered in a function, it immediately exits the function and returns control to the calling code. Any code following the return statement within the same block will not be executed. In many cases, including an ๐—ฒ๐—น๐˜€๐—ฒ ๐˜€๐˜๐—ฎ๐˜๐—ฒ๐—บ๐—ฒ๐—ป๐˜ after a return is redundant and can be safely removed.

โœ… To implement this principle, simply ๐—ฟ๐—ฒ๐—บ๐—ผ๐˜ƒ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ฒ๐—น๐˜€๐—ฒ ๐˜€๐˜๐—ฎ๐˜๐—ฒ๐—บ๐—ฒ๐—ป๐˜ after any return statement. If you need to execute code if the if condition is not met, you can move that code to the end of the function, outside of the if statement.

๐Ÿ’ก ๐—ง๐—ต๐—ฒ ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ is a good choice when you have a simple conditional logic. While it can be used to simplify code in some cases, it should be used judiciously to maintain code readability.

๐Ÿ”ฅ Following the "๐—”๐˜ƒ๐—ผ๐—ถ๐—ฑ ๐—ฒ๐—น๐˜€๐—ฒ ๐—ฎ๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป" principle helps in writing cleaner, more maintainable code by simplifying control flow and improving code readability.

โ” ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ผ๐—ป๐—ฒ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—ฝ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐—ฟ?


๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ ๐—˜๐—ป๐—ฐ๐—ฎ๐—ฝ๐˜€๐˜‚๐—น๐—ฎ๐˜๐—ฒ ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐˜€
โœ”๏ธ ๐—˜๐—ป๐—ฐ๐—ฎ๐—ฝ๐˜€๐˜‚๐—น๐—ฎ๐˜๐—ฒ ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐˜€

โœ… ๐—˜๐—ป๐—ฐ๐—ฎ๐—ฝ๐˜€๐˜‚๐—น๐—ฎ๐˜๐—ฒ ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐˜€ is a practice of clean coding where you replace complex conditional logic with well-named methods to make your code more readable and maintainable.

๐Ÿ”ฅ ๐—ง๐—ต๐—ฒ ๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ผ๐—ณ ๐—ฒ๐—ป๐—ฐ๐—ฎ๐—ฝ๐˜€๐˜‚๐—น๐—ฎ๐˜๐—ถ๐—ป๐—ด ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐˜€:
โ—พ๏ธ ๐—œ๐—บ๐—ฝ๐—ฟ๐—ผ๐˜ƒ๐—ฒ๐˜€ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†: Code reads more like a high-level language, which improves understanding.
โ—พ๏ธ ๐—ฅ๐—ฒ๐—ฑ๐˜‚๐—ฐ๐—ฒ๐˜€ ๐—ฑ๐˜‚๐—ฝ๐—น๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: If a complex condition is used in more than one place, it's better to keep the logic in one place.
โ—พ๏ธ ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ถ๐—ณ๐—ถ๐—ฒ๐˜€ ๐—ฐ๐—ผ๐—ฑ๐—ฒ: It's easier to understand a method call with a well-named method than to understand a complex conditional.
โ—พ๏ธ ๐—˜๐—ฎ๐˜€๐—ถ๐—ฒ๐—ฟ ๐˜๐—ผ ๐˜๐—ฒ๐˜€๐˜: Encapsulated conditionals can be separately tested, ensuring that all edge-cases are covered.

๐Ÿ’ป ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ฒ๐—ป๐—ฐ๐—ฎ๐—ฝ๐˜€๐˜‚๐—น๐—ฎ๐˜๐—ฒ ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐˜€:
To implement encapsulate conditionals, simply extract the conditional logic into a private method. The method should have a clear and concise name that describes what it does.


๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ ๐—ฅ๐—ฒ๐—ฝ๐—น๐—ฎ๐—ฐ๐—ฒ ๐—ถ๐—ณ ๐˜€๐˜๐—ฎ๐˜๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐˜„๐—ถ๐˜๐—ต ๐—ก๐˜‚๐—น๐—น ๐—–๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ
โœ”๏ธ ๐—ฅ๐—ฒ๐—ฝ๐—น๐—ฎ๐—ฐ๐—ฒ ๐—ถ๐—ณ ๐˜€๐˜๐—ฎ๐˜๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐˜„๐—ถ๐˜๐—ต ๐—ก๐˜‚๐—น๐—น ๐—–๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ

โœ… The ๐—ป๐˜‚๐—น๐—น ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ, also known as the null propagation operator or the safe navigation operator, is a feature introduced in C# 6.0 that allows you to write cleaner and more concise code when dealing with potentially null reference types.

๐Ÿ’ก The ๐—ป๐˜‚๐—น๐—น ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ is represented by a question mark followed by a period (?.) and is used to access members or invoke methods on an object that may be null. If the object is null, the expression returns null instead of throwing a null reference exception.

๐Ÿ”ฅ ๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ผ๐—ณ ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ป๐˜‚๐—น๐—น ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ:
โ—พ๏ธThe null conditional operator can make your code more concise and readable.
โ—พ๏ธThe null conditional operator can help to avoid null-reference exceptions.
โ—พ๏ธThe null conditional operator can be used to chain together multiple member or element accesses, even if some of the members or elements may be null.


๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
๐Ÿ’ก๐— ๐—ผ๐—ฑ๐—ถ๐—ณ๐˜†๐—ถ๐—ป๐—ด ๐—ฃ๐—ฎ๐˜€๐˜€๐—ฒ๐—ฑ ๐—ข๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜๐˜€ ๐—ถ๐—ป ๐—–#

โœ… ๐— ๐—ผ๐—ฑ๐—ถ๐—ณ๐˜† ๐˜๐—ต๐—ฒ ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜ ๐˜„๐—ต๐—ฒ๐—ฟ๐—ฒ ๐—บ๐—ฒ๐˜๐—ต๐—ผ๐—ฑโ€™๐˜€ ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป ๐˜๐˜†๐—ฝ๐—ฒ ๐—ถ๐˜€ ๐˜ƒ๐—ผ๐—ถ๐—ฑ:
Modify objects in-place (return type ๐˜ƒ๐—ผ๐—ถ๐—ฑ) for simple state changes.

โœ… ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐—ฎ ๐—ก๐—ฒ๐˜„ ๐—œ๐—ป๐˜€๐˜๐—ฎ๐—ป๐—ฐ๐—ฒ ๐—ฎ๐—ป๐—ฑ ๐—ฅ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป ๐—œ๐˜:
Create and ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป a new instance for immutability or history tracking.

โœ… ๐—จ๐˜€๐—ฒ ๐—ฟ๐—ฒ๐—ณ ๐—ธ๐—ฒ๐˜†๐˜„๐—ผ๐—ฟ๐—ฑ:
Use ๐—ฟ๐—ฒ๐—ณ to modify the original object, being cautious about side effects.

โœ… ๐—จ๐˜€๐—ฒ ๐—ถ๐—ป ๐—ธ๐—ฒ๐˜†๐˜„๐—ผ๐—ฟ๐—ฑ:
Use ๐—ถ๐—ป to pass objects as read-only references.

โœ… ๐—จ๐˜€๐—ฒ ๐—ผ๐˜‚๐˜ ๐—ธ๐—ฒ๐˜†๐˜„๐—ผ๐—ฟ๐—ฑ:
Use ๐—ผ๐˜‚๐˜ to return and initialize objects without prior initialization.


๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด

โœ… ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด is a feature introduced in ๐—–# ๐Ÿต that enhances the pattern matching capabilities of the language. Pattern matching is a way to compare values against patterns.

โœ… ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด specifically allows you to perform pattern matching based on relational comparisons, such as greater than, less than, greater than or equal to, and less than or equal to comparisons.

โœ… It is a ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น programming technique, which means that it focuses on the evaluation of expressions rather than the control flow of your code.
โœ”๏ธ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด

โœ… ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด is a feature introduced in ๐—–# ๐Ÿต that enhances the pattern matching capabilities of the language. Pattern matching is a way to compare values against patterns.

โœ… ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด specifically allows you to perform pattern matching based on relational comparisons, such as greater than, less than, greater than or equal to, and less than or equal to comparisons.

โœ… It is a ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น programming technique, which means that it focuses on the evaluation of expressions rather than the control flow of your code.

๐Ÿ”ฅ ๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ผ๐—ณ ๐—ฟ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฝ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐—บ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด:
โ—พ๏ธ ๐—–๐—ผ๐—ป๐—ฐ๐—ถ๐˜€๐—ฒ๐—ป๐—ฒ๐˜€๐˜€: Relational patterns matching can be used to create more concise and readable code.
โ—พ๏ธ ๐—˜๐˜…๐—ฝ๐—ฟ๐—ฒ๐˜€๐˜€๐—ถ๐˜ƒ๐—ฒ๐—ป๐—ฒ๐˜€๐˜€: Relational patterns matching can be used to express more complex conditions.

๐Ÿ’ก ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€ ๐— ๐—ฎ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด is not always the best solution. Sometimes, it is simpler and more efficient to use the traditional way of checking if an expression matches a certain condition.



๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ ๐—จ๐˜€๐—ฒ '๐— ๐—ถ๐—ป๐—•๐˜†' ๐—ผ๐—ฟ '๐— ๐—ฎ๐˜…๐—•๐˜†' ๐—ถ๐—ป๐˜€๐˜๐—ฒ๐—ฎ๐—ฑ ๐—ผ๐—ณ ๐—ผ๐—ฟ๐—ฑ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด ๐—ฎ๐—ป๐—ฑ ๐˜๐—ฎ๐—ธ๐—ถ๐—ป๐—ด '๐—™๐—ถ๐—ฟ๐˜€๐˜' ๐—ผ๐—ฟ '๐—Ÿ๐—ฎ๐˜€๐˜'

โœ… LINQ ๐— ๐—ถ๐—ป๐—•๐˜† and ๐— ๐—ฎ๐˜…๐—•๐˜† are extension methods in C# that allow you to find the minimum or maximum element in a sequence based on a specified property. They were introduced in .๐—ก๐—˜๐—ง ๐Ÿฒ.

๐Ÿ’ก Simplify LINQ expression by Use ๐— ๐—ถ๐—ป๐—•๐˜† or ๐— ๐—ฎ๐˜…๐—•๐˜† instead of ordering and taking 'First' or 'Last'

๐Ÿ”ฅ ๐—ง๐—ต๐—ฒ ๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ผ๐—ณ ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐—Ÿ๐—œ๐—ก๐—ค ๐— ๐—ถ๐—ป๐—•๐˜† ๐—ฎ๐—ป๐—ฑ ๐— ๐—ฎ๐˜…๐—•๐˜†:
โ—พ๏ธThey are more concise and easier to read.
โ—พ๏ธThey are more efficient, as they do not need to sort the entire sequence.
โ—พ๏ธThey can be used with any type of sequence, including sequences of objects.



๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ ๐—ก๐˜‚๐—น๐—น ๐—–๐—ผ๐—ฎ๐—น๐—ฒ๐˜€๐—ฐ๐—ถ๐—ป๐—ด ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ

โœ… The ๐—ก๐˜‚๐—น๐—น ๐—–๐—ผ๐—ฎ๐—น๐—ฒ๐˜€๐—ฐ๐—ถ๐—ป๐—ด ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ (??) in C# is used to provide a concise way of handling null values. It's particularly useful when you want to provide a default value when a nullable expression evaluates to null. This operator helps make your code cleaner by reducing the need for verbose null checks and conditional statements.
๐Ÿ’Ž ๐—–๐—น๐—ฒ๐—ฎ๐—ป ๐—–๐—ผ๐—ฑ๐—ฒ
โœ”๏ธ ๐—ก๐˜‚๐—น๐—น ๐—–๐—ผ๐—ฎ๐—น๐—ฒ๐˜€๐—ฐ๐—ถ๐—ป๐—ด ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ

โœ… The ๐—ก๐˜‚๐—น๐—น ๐—–๐—ผ๐—ฎ๐—น๐—ฒ๐˜€๐—ฐ๐—ถ๐—ป๐—ด ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ (??) in C# is used to provide a concise way of handling null values. It's particularly useful when you want to provide a default value when a nullable expression evaluates to null. This operator helps make your code cleaner by reducing the need for verbose null checks and conditional statements.

๐Ÿ”ฅ ๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ผ๐—ณ ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ป๐˜‚๐—น๐—น ๐—ฐ๐—ผ๐—ฎ๐—น๐—ฒ๐˜€๐—ฐ๐—ถ๐—ป๐—ด ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ:
โ—พ๏ธ ๐—–๐—ผ๐—ป๐—ฐ๐—ถ๐˜€๐—ฒ ๐—–๐—ผ๐—ฑ๐—ฒ: It reduces the need for writing lengthy null-checking code, resulting in more compact and clear expressions.
โ—พ๏ธ ๐—ฅ๐—ฒ๐—ฎ๐—ฑ๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†: The operator is well-known among C# developers, so it improves the readability of your code by providing a common and recognizable pattern.
โ—พ๏ธ ๐—”๐˜ƒ๐—ผ๐—ถ๐—ฑ๐—ถ๐—ป๐—ด ๐——๐˜‚๐—ฝ๐—น๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: It eliminates the need to repeat the same null-checking logic throughout your codebase.
โ—พ๏ธ ๐——๐—ฒ๐—ณ๐—ฎ๐˜‚๐—น๐˜ ๐—ฉ๐—ฎ๐—น๐˜‚๐—ฒ๐˜€: It's particularly useful when you want to provide default values for nullable variables.



๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ ๐—ฅ๐—ฒ๐˜๐—ต๐—ฟ๐—ผ๐˜„๐—ถ๐—ป๐—ด ๐—˜๐˜…๐—ฐ๐—ฒ๐—ฝ๐˜๐—ถ๐—ผ๐—ป๐˜€
โœ”๏ธ ๐—ฅ๐—ฒ๐˜๐—ต๐—ฟ๐—ผ๐˜„๐—ถ๐—ป๐—ด ๐—˜๐˜…๐—ฐ๐—ฒ๐—ฝ๐˜๐—ถ๐—ผ๐—ป๐˜€

โŒ Exceptions should not be explicitly rethrown because it can lead to loss of information about the original exception. When an exception is thrown, it creates a stack trace that records the call stack at the point where the exception was thrown. This stack trace can be helpful for debugging the exception.

โœ… The preferred way to handle an exception is to catch it and then take some action to resolve the problem. This could involve logging the exception, displaying an error message to the user, or taking some other corrective action. If the exception cannot be resolved, it should be rethrown, but without specifying the exception explicitly. This will preserve the original stack trace information and make debugging easier.

๐Ÿ’ก When you want to wrap the exception in a different exception type. In this case, you should create a new exception object and then throw that object. Do not simply rethrow the original exception object.


๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ๐—ฆ๐˜„๐—ถ๐˜๐—ฐ๐—ต ๐—˜๐˜…๐—ฝ๐—ฟ๐—ฒ๐˜€๐˜€๐—ถ๐—ผ๐—ป
โœ”๏ธ๐—ฆ๐˜„๐—ถ๐˜๐—ฐ๐—ต ๐—˜๐˜…๐—ฝ๐—ฟ๐—ฒ๐˜€๐˜€๐—ถ๐—ผ๐—ป

๐Ÿ•ฏ The ๐˜€๐˜„๐—ถ๐˜๐—ฐ๐—ต ๐˜€๐˜๐—ฎ๐˜๐—ฒ๐—บ๐—ฒ๐—ป๐˜ has been part of C# since its early versions. It allows you to evaluate an expression against a series of case values and execute code blocks based on the matched case.
Each case value must be a constant value that is known at compile-time. After a case block is executed, you usually need to include a break statement to exit the switch statement.

๐Ÿ’ก The ๐˜€๐˜„๐—ถ๐˜๐—ฐ๐—ต ๐—ฒ๐˜…๐—ฝ๐—ฟ๐—ฒ๐˜€๐˜€๐—ถ๐—ผ๐—ป was introduced in C# 8 as a more concise and expressive alternative to the traditional switch statement. It allows you to assign a value to a variable based on the value of an expression.
In a switch expression, you use the => syntax to specify the value to assign if the expression matches a certain case. The _ is a discard symbol and is used as the "default" case.

โœ… Both the ๐˜€๐˜„๐—ถ๐˜๐—ฐ๐—ต ๐˜€๐˜๐—ฎ๐˜๐—ฒ๐—บ๐—ฒ๐—ป๐˜ and the ๐˜€๐˜„๐—ถ๐˜๐—ฐ๐—ต ๐—ฒ๐˜…๐—ฝ๐—ฟ๐—ฒ๐˜€๐˜€๐—ถ๐—ผ๐—ป are used for similar purposes, the switch expression offers more concise syntax and greater flexibility for pattern matching and value assignment, making it a more powerful tool for modern C# development.


๐Ÿค” Which one do you prefer?


๐—ง๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐Ÿ˜Š
โœ”๏ธ One Dot Per Line
โœ”๏ธ One Dot Per Line

โœ… "One dot per line" is a principle of writing clean code in C# (and other programming languages) that suggests breaking chained method calls and property accesses onto separate lines, placing each dot on a new line. This helps to enhance code readability and maintainability by making the code structure clear and easier to follow.


โœ…Advantages of using "One dot per line" approach:

- Improved Readability: By placing each dot on a separate line, the code becomes more readable, especially when dealing with long chains of method calls or nested property access.

- Reduced Horizontal Scrolling: Code lines become shorter, reducing the need for horizontal scrolling while reading the code, which is often considered less pleasant and harder to follow.

- Easier Debugging: With each method call or property access on a separate line, it becomes easier to set breakpoints and debug specific parts of the code.

- Easier Code Review: The code becomes more structured, making it easier for colleagues to review and provide feedback on the code.


โœ…The "One dot per line" rule is a useful coding practice that can help to improve the readability, maintainability, and overall quality of your code.


Thank you for reading ๐Ÿ˜Š
โœ”๏ธ C#: Exceptions should not be thrown in finally blocks
โœ”๏ธ C#: Exceptions should not be thrown in finally blocks

โœณ๏ธIf an exception is already being thrown within the try block or caught in a catch block, throwing another exception in the finally block will override the original exception. This means that the original exceptionโ€™s message and stack trace will be lost, potentially making it challenging to diagnose and troubleshoot the root cause of the problem.


โœณ๏ธHere are a few reasons why throwing exceptions from finally blocks is discouraged:

- Masking the original exception: If an exception is thrown in a finally block, it can overwrite the original exception that caused the try block to exit. This can make it harder to debug the actual cause of the problem, as you won't have access to the original exception information.

- Unpredictable behavior: When an exception is thrown from a finally block, it may interfere with the exception handling mechanism itself. For example, if the catch block outside the finally block handles the exception, throwing another exception from finally will disrupt the expected control flow.

- Resource leaks: The primary purpose of a finally block is to release resources or perform cleanup tasks. If an exception is thrown from the finally block, these cleanup operations may not be completed, leading to resource leaks or inconsistent program state.


โœณ๏ธInstead of throwing exceptions from finally blocks, it's better to handle exceptions appropriately within the try, catch, and finally blocks. If an exception occurs in the finally block, it's usually best to log the error or take some alternative action to ensure proper cleanup without disrupting the main exception handling flow.


Thank you for reading ๐Ÿ˜Š