Why does the first one work but not the second one? Is there anything to worry about, when converting objects to structs?
struct Hex { /* ... */ }
object actionData = GetData();
1:
bool isHex = actionData is Hex;
Hex hexx = (Hex)actionData;
2:
Hex hex = actionData as Hex;
>Solution :
Because as operator returns null if type check fails:
The expression of the form
E as T
whereEis an expression that returns a value andTis the name of a type or a type parameter, produces the same result as:E is T ? (T)(E) : (T)nullexcept thatEis only evaluated once.
Which can’t be done if T is non-nullable value type (so compiler emits the error). Using nullable value type as T will work:
object actionData = new Hex(); // your code returning boxed Hex
Hex? hex = actionData as Hex?;
if(hex.HasValue)
{
...
}