assure compiler type is not null
i have a code that early returns if a property is null. how do i tell the compiler the property is not null afterwards?
20 Replies
what is this called? and where can i learn about this
Just a part of pattern matching
this tells it that it is not null
there is nothing more you need to do
variables declared as
var
's always shows up as nullable in inspection. That doesn't mean the compiler sees it as nullable, if that makes sense?whether the variable is declared as nullable is different from whether it is null right now
are you on an updated version of the C# extension? it should tell you this
this is mine
well, yes...
if it's not null, you return
so, if you didn't return, it's null
wait... what am i doing
if you change it to
is null
that the warning will go awayok i have another one
public Range? Location { get; init; }
yes, that's because Range is a struct
ohhh
(probably)
it means that
Range?
is a Nullable<Range>
which means, you need to unrap the value manually
you can do it using range.HasValue
, with range.Value
or range.GetValueOrDefault()
you can also use the pattern matching
if (Location is not Range r)
or if (Location is not { } r)
thanks!
can i use this pattern matching on the FilePath too?
if (FilePath is not string path)
yes
FilePath is
string?
ok nice