✔️ 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗱𝗼 𝗼𝗻𝗲 𝘁𝗵𝗶𝗻𝗴
✅ This principle suggests that a function should have only one reason to change, meaning that it should perform a single, well-defined task and not multiple unrelated tasks. This approach increases readability, maintainability, and testability of your code.
❌ A function that does too many things or has too many responsibilities can become difficult to understand, test, and maintain.
𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻:
◾️ Identify the single responsibility of the function. What is the one thing that the function should do?
◾️ Extract all unrelated code from the function. This code can be moved to other functions or classes.
◾️ Give the function a descriptive name that reflects its single responsibility.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✅ This principle suggests that a function should have only one reason to change, meaning that it should perform a single, well-defined task and not multiple unrelated tasks. This approach increases readability, maintainability, and testability of your code.
❌ A function that does too many things or has too many responsibilities can become difficult to understand, test, and maintain.
𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻:
◾️ Identify the single responsibility of the function. What is the one thing that the function should do?
◾️ Extract all unrelated code from the function. This code can be moved to other functions or classes.
◾️ Give the function a descriptive name that reflects its single responsibility.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✔️ 𝗨𝘀𝗲 𝘀𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗧𝗼𝗨𝗽𝗽𝗲𝗿()/𝗧𝗼𝗟𝗼𝘄𝗲𝗿() 𝘄𝗵𝗲𝗻 𝗰𝗼𝗺𝗽𝗮𝗿𝗶𝗻𝗴 𝘀𝘁𝗿𝗶𝗻𝗴𝘀
🐌 Using 𝗧𝗼𝗨𝗽𝗽𝗲𝗿() and 𝗧𝗼𝗟𝗼𝘄𝗲𝗿() for case conversion in C# can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions.
🚀 𝗦𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 is faster than ToUpper() or ToLower() due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.
🔥 To perform string comparison , it's better to use the built-in comparison methods like 𝗦𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 with appropriate StringComparison options, which handle case-insensitivity and cultural considerations correctly while maintaining better performance and accuracy.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
#csharp #dotnet #programming #cleancode
🐌 Using 𝗧𝗼𝗨𝗽𝗽𝗲𝗿() and 𝗧𝗼𝗟𝗼𝘄𝗲𝗿() for case conversion in C# can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions.
🚀 𝗦𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 is faster than ToUpper() or ToLower() due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.
🔥 To perform string comparison , it's better to use the built-in comparison methods like 𝗦𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 with appropriate StringComparison options, which handle case-insensitivity and cultural considerations correctly while maintaining better performance and accuracy.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
#csharp #dotnet #programming #cleancode
👍1
✔️ 𝗔𝘃𝗼𝗶𝗱 𝗲𝗹𝘀𝗲 𝗮𝗳𝘁𝗲𝗿 𝗿𝗲𝘁𝘂𝗿𝗻
❌ 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.
❔ 𝗪𝗵𝗶𝗰𝗵 𝗼𝗻𝗲 𝗱𝗼 𝘆𝗼𝘂 𝗽𝗿𝗲𝗳𝗲𝗿?
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
❌ 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
✅ 𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
✅ 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
✅ 𝗠𝗼𝗱𝗶𝗳𝘆 𝘁𝗵𝗲 𝗼𝗯𝗷𝗲𝗰𝘁 𝘄𝗵𝗲𝗿𝗲 𝗺𝗲𝘁𝗵𝗼𝗱’𝘀 𝗿𝗲𝘁𝘂𝗿𝗻 𝘁𝘆𝗽𝗲 𝗶𝘀 𝘃𝗼𝗶𝗱:
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.
✔️𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴
✅ 𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
✅ 𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
✅ 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.
💎 𝗖𝗹𝗲𝗮𝗻 𝗖𝗼𝗱𝗲
✔️ 𝗡𝘂𝗹𝗹 𝗖𝗼𝗮𝗹𝗲𝘀𝗰𝗶𝗻𝗴 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿
✅ 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
✔️ 𝗡𝘂𝗹𝗹 𝗖𝗼𝗮𝗹𝗲𝘀𝗰𝗶𝗻𝗴 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿
✅ 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.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
❌ 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?
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
🕯 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?
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊