C# Tips. Top 5 tips to improve you code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi there! Today I am gonna showing five tips in C# which can help you to improve your code. Let's get started! Tip number one. Null coalescing operator This is an important operator in C#. It returns the left-hand operand if the operand is not null; otherwise, it returns the right operand. In other words operator is used to assign a default value to a variable when the value is null. Let's look at example. The output here is Johnny, because the variable name is null and the operator checks for a null value. If it is null, then it will assign the defau lt value Johnny. In second example we will get another result - Andrew, because variable name is not null. In property also we can use a null Coalescing operator like this. Write in the comments if you used this operator Tip number two. String Interpolation C# string interpolation is a method of concatenating, formatting and manipulating strings. This feature was introduced in C# 6. Using string interpolation, we can use objects and expressions as a part of the string interpolation operation. Syntax of string interpolation starts with a dollar sign and expressions are defined within a brackets. I really recommend to use this way of defining string. Tip number three. Null-conditional operator Null checking is important part of developing quality code, Without it Code with throw NullReferenceException Consider this code. It throws a NullReferenceException Most common way to fix it is using If. However, using if statements to check for null references can make the code more verbose. Another way to solve this issue is to use a null-conditional operator The part to the right of the operator only evaluates if the part to the left is not null. Otherwise, the code returns null. So, in the case, person?.Name evaluates to null, but it does not throw an exception because there is no attempt to access a member on a null reference Tip number four. Property Initializers Property initializers lets you declare an initial value for a property: A benefit of using property initializers is that you can not declare a setter, thus making the property immutable. Property initializers works great together with C# 6 primary constructor syntax. Tip number five. Yield-keyword This keyword lets you feed an IEnumerable-interface with items. The following example will return each powers of 2 up to the exponent of 8 (e.g. 2, 4, 8, 16, 32, 64, 128 ,256): yield return can be very powerful if it’s used in the correct way. It enables you to lazily generate a sequence of objects, when the system doesn't have to enumerate the whole collection – it can be done on demand. Hope you enjoyed watching this video. Don't forget to subscribe and hit like button and happy coding!
Info
Channel: Coding Tips
Views: 398
Rating: undefined out of 5
Keywords: c#, codingtips
Id: w8xnvDnllQU
Channel Id: undefined
Length: 2min 45sec (165 seconds)
Published: Sat Jun 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.