C# Null-Conditional Operator (? Operator) Explained!

  Рет қаралды 6,489

Gerald Versluis

Gerald Versluis

Күн бұрын

Пікірлер: 21
@WhitefoxSpace
@WhitefoxSpace 2 жыл бұрын
Dude you are an *incredible* teacher.
@jfversluis
@jfversluis 2 жыл бұрын
Thank you so much! 🥰
@jaimemenendezalvarez5796
@jaimemenendezalvarez5796 3 жыл бұрын
I came here to resolve a question and I left with another. I did not know that it was possible to define an object of the same class within the definition of the class itself. Either way, thanks for resolving my concerns about Null-conditional operators
@jfversluis
@jfversluis 3 жыл бұрын
Haha the more you know... Glad you enjoyed it! Any other questions you might have?
@johnb2572
@johnb2572 3 жыл бұрын
Nice, very useful. Thank you.
@jfversluis
@jfversluis 3 жыл бұрын
Glad it was helpful!
@itamarcus
@itamarcus 3 жыл бұрын
good tips... and now we also know that Gerald loves gardening ^_^
@jfversluis
@jfversluis 3 жыл бұрын
Hahaha actually… he does not 😜
@klasseka
@klasseka 3 жыл бұрын
I often use combo of operators ?. and ?? . It`s useful if need to react on null like Console.WriteLine(person?.Name ?? "A person has no name");
@jfversluis
@jfversluis 3 жыл бұрын
That makes total sense! I didn't want to take on too much in one video, but more on this is coming :)
@BijouBakson
@BijouBakson 3 жыл бұрын
Thank you.
@jfversluis
@jfversluis 3 жыл бұрын
Thanks for watching!
@dolphinsoft_Youtube
@dolphinsoft_Youtube 3 жыл бұрын
perfect
@jfversluis
@jfversluis 3 жыл бұрын
Thanks!
@hrishikeshgarud7177
@hrishikeshgarud7177 3 жыл бұрын
Nice website.
@jfversluis
@jfversluis 3 жыл бұрын
Glad you like it!
@MrBigdogtim69
@MrBigdogtim69 3 жыл бұрын
Does it make sense to declare properties with the ? operator? For example, public class foo{ public string bar? {get; set; } }. If so - how do you assign to bar?
@jfversluis
@jfversluis 3 жыл бұрын
Great question Tim! And in fact this doesn't really have much to do with the thing I'm showing here. It is somewhat related. By declaring a member like that you make it nullable when it usually isn't. Like in your example, a string gets assigned the default value. In the case of a string the default value is null already though because it’s a reference type. But for value types it will have a default value. By adding the question mark, it now is nullable, so the default value will be null and you can assign null to it. To be honest I don't find myself needing it that often, but it comes in handy sometimes. Have you used it? More info here: docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/nullable-value-types
@--Eric--
@--Eric-- 3 жыл бұрын
@Tim Belvin Probably a copy/paste error but the '?' goes on the type, not on the variable name. 'string? bar' The '?' has nothing to do with `how´ you set a variable, it only adds a value (null) you are allowed to use. valid 'int' values: -2147483648,...,-2,-1,0,1,2,...,2147483647 valid 'int?' values: -2147483648,...,-2,-1,0,1,2,...,2147483647 and null 'string' is only allowed to have 0-length strings "", up to n-length "..." 'string?" can besides the above x-length strings also be set to null It's up to you if you want to have this extra null-value to use for some non existent value or error etc. It may be a different thing for you if bar is "" or null (i.e. is "" an error or not). /* The above is not strictly correct, it's possible to get around, but this is the spirit of '?' nullable value types. */
@jfversluis
@jfversluis 3 жыл бұрын
@@--Eric-- Ah you're right :) my answer only makes sense if it goes on the type not the variable name. Overlooked that the first time
@jfversluis
@jfversluis 3 жыл бұрын
You’re right! Updated it a bit
Working with Null in .NET 6 and C# 10
57:26
IAmTimCorey
Рет қаралды 36 М.
Stackalloc and Spans
30:17
Coding Tutorials
Рет қаралды 12 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
C# Yield Return: What is it and how does it work?
15:09
Brian Lagunas
Рет қаралды 58 М.
C# Nullable reference types - No more null reference exceptions!
18:06
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 437 М.
Select Different Camera and Prevent Memory Leaks with CameraView
10:50
Gerald Versluis
Рет қаралды 2,5 М.
No more null exceptions in C# with null-coalescing, ?? and ??= operators
15:47
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 264 М.
Creating Blazor Reusable Components (Server & WebAssembly)
17:24
Gerald Versluis
Рет қаралды 7 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН