C
C#6mo ago
Esperida

Who to force a type

Hello, In a C# class i have this property public Type? Value1; And on the same class have this methode
public void test()
{
if (Value1 != null)
{
Type Value2 = Value1;
}
}
public void test()
{
if (Value1 != null)
{
Type Value2 = Value1;
}
}
C# would not compile because Type1 could be null and not Value2 How to edit test for granted Value1 is of type Type1 ? I have try Value1.HasValue in the if but that do the same Thanks at advence
4 Replies
Pobiega
Pobiega6mo ago
Value1.Value
Angius
Angius6mo ago
public void test()
{
if (Value1 is {} v)
{
Type Value2 = v;
}
}
public void test()
{
if (Value1 is {} v)
{
Type Value2 = v;
}
}
Esperida
EsperidaOP6mo ago
Oh well thanks, its compile Do the same
Pobiega
Pobiega6mo ago
Prefer ZZZZs solution, it's much nicer.
Want results from more Discord servers?
Add your server