Coder Baba
2.41K 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
✔️ 𝗡𝘂𝗹𝗹 𝗮𝗿𝗴𝘂𝗺𝗲𝗻𝘁 𝗰𝗵𝗲𝗰𝗸𝘀 𝗶𝗻 𝗖# 𝟭𝟬 𝗮𝗻𝗱 .𝗡𝗘𝗧 𝟲

The 𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝗡𝘂𝗹𝗹𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻.𝗧𝗵𝗿𝗼𝘄𝗜𝗳𝗡𝘂𝗹𝗹 method is a static method in the System namespace that throws an 𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝗡𝘂𝗹𝗹𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻 if the specified object is null. It is a convenient way to check for null parameters in your code. It can help to prevent runtime errors and make the code more concise.

🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝗡𝘂𝗹𝗹𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻.𝗧𝗵𝗿𝗼𝘄𝗜𝗳𝗡𝘂𝗹𝗹:
◾️ It is easy to use. Just pass the object you want to check for null to the method, and it will throw an exception if the object is null.
◾️ It is reduce code size and make it easy to read.

🤔 Which one do you prefer?


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✔️𝗣𝗿𝗼𝗽𝗲𝗿𝘁𝘆 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 in C#

It is allows you to test whether an expression's properties or fields match specific values or nested patterns. It's a concise and expressive way to perform conditional logic based on object structures.

🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀:
◾️ 𝗖𝗼𝗻𝗰𝗶𝘀𝗲𝗻𝗲𝘀𝘀: Can be used to create more concise and readable code.
◾️ 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲𝗻𝗲𝘀𝘀: Can be used to express more complex conditions.
◾️ 𝗧𝘆𝗽𝗲 𝘀𝗮𝗳𝗲𝘁𝘆: Ensures type correctness at compile time, reducing potential runtime errors.

🤔 Which one do you prefer?


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
💡𝗖# 𝗧𝗶𝗽
✔️ 𝗣𝗮𝗿𝗮𝗹𝗹𝗲𝗹 𝗙𝗼𝗿𝗘𝗮𝗰𝗵
✔️ 𝗣𝗮𝗿𝗮𝗹𝗹𝗲𝗹 𝗙𝗼𝗿𝗘𝗮𝗰𝗵

🐌 The regular 𝗳𝗼𝗿𝗲𝗮𝗰𝗵 loop is a sequential construct. It iterates over a collection or an enumerable in a single-threaded manner, processing each element one after the other. It's a good choice when the tasks performed inside the loop are relatively simple and quick to execute, and there's no need to parallelize them.

🚀 The 𝗣𝗮𝗿𝗮𝗹𝗹𝗲𝗹.𝗙𝗼𝗿𝗘𝗮𝗰𝗵 construct is part of the System.Threading.Tasks namespace and is designed for parallel execution of tasks across multiple threads. It divides the input collection into smaller partitions and processes them concurrently on separate threads.

The performance of 𝗳𝗼𝗿𝗲𝗮𝗰𝗵 and 𝗣𝗮𝗿𝗮𝗹𝗹𝗲𝗹.𝗙𝗼𝗿𝗘𝗮𝗰𝗵 depends on the specific scenario. In general, Parallel.ForEach can be faster than foreach if the collection is large and the work being done is well-suited for parallel execution. However, there are some cases where foreach may be faster than Parallel.ForEach.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✔️𝗨𝘀𝗲 𝘆𝗶𝗲𝗹𝗱 𝗿𝗲𝘁𝘂𝗿𝗻 𝘁𝗼 𝗺𝗶𝗻𝗶𝗺𝗶𝘇𝗲 𝗺𝗲𝗺𝗼𝗿𝘆 𝘂𝘀𝗮𝗴𝗲
💡𝗖# 𝗧𝗶𝗽
✔️𝗨𝘀𝗲 𝘆𝗶𝗲𝗹𝗱 𝗿𝗲𝘁𝘂𝗿𝗻 𝘁𝗼 𝗺𝗶𝗻𝗶𝗺𝗶𝘇𝗲 𝗺𝗲𝗺𝗼𝗿𝘆 𝘂𝘀𝗮𝗴𝗲

The 𝘆𝗶𝗲𝗹𝗱 keyword is used in an iterator block to provide a value to the enumerator object or to signal the end of iteration. When used with return, it provides a value, and when used with break, it signals the end of iteration.

The 𝘆𝗶𝗲𝗹𝗱 keyword is often used for custom iteration over a collection. It's a powerful tool for writing more efficient code. It's worth noting that yield can only be used in the body of methods, operators, or accessors, and those have to return either IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.

🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝘂𝘀𝗶𝗻𝗴 𝘆𝗶𝗲𝗹𝗱:
◾️𝗗𝗲𝗳𝗲𝗿𝗿𝗲𝗱 𝗲𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻: The code in an iterator block (where yield return is used) is not executed until the sequence is enumerated. This can lead to performance benefits because it allows elements to be generated on demand rather than all at once.
◾️𝗦𝗶𝗺𝗽𝗹𝗲𝗿 𝗰𝗼𝗱𝗲: When creating a collection that needs to be iterated over, yield can simplify your code by abstracting away the need to create and manage your own collection.
◾️𝗠𝗲𝗺𝗼𝗿𝘆 𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝗰𝘆: When iterating over large collections or sequences, yield can be more memory efficient because it does not require the entire collection to be held in memory at once.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
1
✔️𝗨𝘀𝗲 𝗻𝗮𝗺𝗲𝗼𝗳() 𝘁𝗼 𝗰𝗼𝗻𝘃𝗲𝗿𝘁 𝗮𝗻 𝗲𝗻𝘂𝗺 𝘁𝗼 𝗮 𝘀𝘁𝗿𝗶𝗻𝗴
💡𝗖# 𝗧𝗶𝗽
✔️𝗨𝘀𝗲 𝗻𝗮𝗺𝗲𝗼𝗳() 𝘁𝗼 𝗰𝗼𝗻𝘃𝗲𝗿𝘁 𝗮𝗻 𝗲𝗻𝘂𝗺 𝘁𝗼 𝗮 𝘀𝘁𝗿𝗶𝗻𝗴

The 𝗧𝗼𝗦𝘁𝗿𝗶𝗻𝗴 method, inherited from the System.Object class, is used to convert a value into its string representation. It can be overridden in derived classes to provide a meaningful string representation of the object's current state.

The 𝗻𝗮𝗺𝗲𝗼𝗳 keyword, introduced in C# 6.0, is used to get the name of a variable, type, or member as a string at compile-time. It's useful to avoid magic strings in your code and to keep your code refactor-friendly.

🚀 𝗻𝗮𝗺𝗲𝗼𝗳() is the preferred way to convert an enum to a string. This is because nameof() is evaluated at compile time and will inject a string literal that will never change, while ToString() is evaluated at runtime. This means that nameof() is more efficient and can help to improve the performance of your code.

💡 Using nameof() with enums can lead to mismatched enum member names if the enum changes without recompiling referencing projects, as nameof() provides names at compile-time.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
1
✔️ 𝗗𝗲𝗰𝗹𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝗣𝗮𝘁𝘁𝗲𝗿𝗻

You use declaration and type patterns to check if the run-time type of an expression is compatible with a given type. With a 𝗱𝗲𝗰𝗹𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝗽𝗮𝘁𝘁𝗲𝗿𝗻, you can also declare a new local variable. When a declaration pattern matches an expression, that variable is assigned a converted expression result.

🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝘂𝘀𝗶𝗻𝗴 𝗱𝗲𝗰𝗹𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝗽𝗮𝘁𝘁𝗲𝗿𝗻𝘀:
◾️ They can make your code more readable and maintainable.
◾️ They can help you to avoid errors caused by type mismatches.
◾️ They can be used to write more concise and expressive code.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
1
✔️ 𝗨𝘀𝗲 𝘀𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗧𝗼𝗨𝗽𝗽𝗲𝗿()/𝗧𝗼𝗟𝗼𝘄𝗲𝗿() 𝘄𝗵𝗲𝗻 𝗰𝗼𝗺𝗽𝗮𝗿𝗶𝗻𝗴 𝘀𝘁𝗿𝗶𝗻𝗴𝘀

🐌 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.
✔️ 𝗥𝗲𝗽𝗹𝗮𝗰𝗲 𝗶𝗳 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 𝘄𝗶𝘁𝗵 𝗡𝘂𝗹𝗹 𝗖𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿
✔️ 𝗥𝗲𝗽𝗹𝗮𝗰𝗲 𝗶𝗳 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 𝘄𝗶𝘁𝗵 𝗡𝘂𝗹𝗹 𝗖𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿

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.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
1
✔️ 𝗖# 𝟭𝟮 𝗣𝗿𝗶𝗺𝗮𝗿𝘆 𝗖𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀

𝗖# 𝟭𝟮 introduces 𝗽𝗿𝗶𝗺𝗮𝗿𝘆 𝗰𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀, a concise syntax to declare constructors whose parameters are available anywhere in the body of the type.

𝗧𝗵𝗲 𝗺𝗼𝘀𝘁 𝗰𝗼𝗺𝗺𝗼𝗻 𝘂𝘀𝗲𝘀 𝗳𝗼𝗿 𝗮 𝗽𝗿𝗶𝗺𝗮𝗿𝘆 𝗰𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿 𝗽𝗮𝗿𝗮𝗺𝗲𝘁𝗲𝗿 𝗮𝗿𝗲:
◾️ As an argument to a base() constructor invocation.
◾️ To initialize a member field or property.
◾️ Referencing the constructor parameter in an instance member.

🔥 You can use the 𝗽𝗿𝗶𝗺𝗮𝗿𝘆 𝗰𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀 as best suits your design. For classes and structs, primary constructor parameters are parameters to a constructor that must be invoked. You can use them to initialize properties. You can initialize fields. Those properties or fields can be immutable, or mutable. You can use them in methods.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✔️ 𝗣𝗿𝗲𝗳𝗲𝗿 𝗔𝗻𝘆() 𝗼𝘃𝗲𝗿 𝗖𝗼𝘂𝗻𝘁()
✔️ 𝗣𝗿𝗲𝗳𝗲𝗿 𝗔𝗻𝘆() 𝗼𝘃𝗲𝗿 𝗖𝗼𝘂𝗻𝘁()

The 𝗖𝗼𝘂𝗻𝘁() method is used to get the total number of elements in a collection that satisfy a given condition. It returns an integer value representing the count of matching elements.

The 𝗔𝗻𝘆() method is used to quickly determine if a collection contains any elements that satisfy a given condition. It returns a boolean value (true if any element matches the condition, otherwise false).

🚀 Using 𝗔𝗻𝘆() over 𝗖𝗼𝘂𝗻𝘁() can be more efficient when you are only interested in determining whether any element matching a condition exists in the collection. This is because Any() stops iterating through the collection as soon as it finds the first matching element, while Count() iterates through the entire collection to count all matching elements. In cases where the collection is large, Any() can potentially provide better performance.

🔥 Use 𝗔𝗻𝘆() over 𝗖𝗼𝘂𝗻𝘁() when you only care if there are any elements in a collection, not the exact number of elements. You should also use Any() when the collection is large or lazy-evaluated. And you should use Any() when you want to improve the readability 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.

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.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✔️ 𝗗𝗼𝗻'𝘁 𝘂𝘀𝗲 𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 𝗰𝗮𝗹𝗹𝘀
✔️ 𝗗𝗼𝗻'𝘁 𝘂𝘀𝗲 𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 𝗰𝗮𝗹𝗹𝘀

⚠️ The 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 operator is used to sort a sequence of elements based on a specified key. When multiple OrderBy calls are chained together, each subsequent call completely reorders the list, discarding the results of the previous call. This means that only the last OrderBy call will have any effect on the final ordering of the sequence.

🐌 Using multiple 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 calls can also lead to performance problems. This is because each OrderBy call performs a full sort of the list, which can be expensive. If you are chaining multiple OrderBy calls together, you are essentially performing multiple full sorts, which can significantly slow down your code.

The 𝗧𝗵𝗲𝗻𝗕𝘆 method is used after the initial OrderBy to apply additional sorting conditions. This ensures that the data is sorted first by the first field (Name in this example), and then by the second field (Price in this example).

🚀 The 𝗧𝗵𝗲𝗻𝗕𝘆 method in C# can be used to chain multiple sorting criteria together, without the performance overhead of calling OrderBy multiple times.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
✔️ 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗻𝗼𝘁 𝗯𝗲 𝗰𝗼𝗻𝗰𝗮𝘁𝗲𝗻𝗮𝘁𝗲𝗱 𝘂𝘀𝗶𝗻𝗴 '+' 𝗶𝗻 𝗮 𝗹𝗼𝗼𝗽
✔️ 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗻𝗼𝘁 𝗯𝗲 𝗰𝗼𝗻𝗰𝗮𝘁𝗲𝗻𝗮𝘁𝗲𝗱 𝘂𝘀𝗶𝗻𝗴 '+' 𝗶𝗻 𝗮 𝗹𝗼𝗼𝗽

🐌 Strings are immutable, which means that once a string object is created, it cannot be modified. When you concatenate strings using the '+' 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 in a loop, a new string object is created at each iteration, and the previous objects are discarded. This can lead to performance issues, especially when dealing with large strings or a large number of iterations.

🚀 A more efficient approach to string concatenation in C# is to use the 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 𝗰𝗹𝗮𝘀𝘀, which is designed for efficiently building strings in a loop. StringBuilder allows you to append strings without creating new objects each time, which leads to better performance.

💡 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 is more useful when dealing with large strings or a large number of iterations and when we have an unknown amount of strings.

🔥 By using 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿, you can significantly reduce memory allocations and improve the performance of your code when you need to concatenate strings in a loop. It is a best practice to use StringBuilder when working with dynamic string building operations.


𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖