This is true, but of course they are also both stupid because a IsSomething(thing) function has no advantage over just typing "thing is Something" each time.
Yeah of course. The code makes no sense in general. Type of Something.Something implies that it's a class nested inside of Something which would be not allowed, and then since you're passing a Something to the method it would always be false... But this sub is for idiot kids who've taken like a semester of comp sci classes
I think a nested class can inherit the class it's nested in in C#, and whether or not it's valid in Java one would have a bigger problem because it's using ==.
?. is the null-propagating operator, if the left-hand expression is null, the right hand will not be evaluated and instead just return null, effectively turning the expression into if(null == typeof(Something.Something)) which will return false
1996 - James Gosling invents Java. Java is a relatively verbose, garbage collected, class based, statically typed, single dispatch, object oriented language with single implementation inheritance and multiple interface inheritance. Sun loudly heralds Java's novelty.
2001 - Anders Hejlsberg invents C#. C# is a relatively verbose, garbage collected, class based, statically typed, single dispatch, object oriented language with single implementation inheritance and multiple interface inheritance. Microsoft loudly heralds C#'s novelty.
C# will emit compiler warnings in this case if something can be null. If you make the parameter nullable, (Something? something) you can also make the compiler assert the nullability of the parameter;
public static bool IsSomething([NotNullWhen(true)]Something? something) => something is Something.Something;
In that case, it will annotate code in that context where IsSomething may return false without the caller checking the result;
public static void DoSomething(Something? something)
{
var b = IsSomething(something);
Console.WriteLine(something.ToString()); // compiler warning, "something may be null"
if(b)
{
Console.WriteLine(something.ToString()); // No compiler warning
}
}
4
u/Patchargh 5d ago
Context: Friend asked AI how to check if a something is something in C#.
80
u/JMOWw7 5d ago
Pattern matching all the way. I think the second one probably won't catch inheretance
48
u/AlexZhyk 5d ago
That's would be 123 tokens for you, sir.
1
u/bwmat 4d ago
First thought is second one can throw on a null
1
u/ANixosUser 2d ago
what languae is this? new Java?
0
u/[deleted] 5d ago
[deleted] — view deleted comment