Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
object to struct with as operator

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;
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Because as operator returns null if type check fails:

The expression of the form E as T where E is an expression that returns a value and T is the name of a type or a type parameter, produces the same result as: E is T ? (T)(E) : (T)null except that E is 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)
{
   ...
}
over 4 years ago · Santiago Trujillo Report

0

Case 1

Assuming that the result of GetData() is of correct the correct type (ie something that is castable to the Hex struct the following will successfully compile and also work at runtime

struct Hex { ...} 
...
Hex foo = (Hex)GetData(); 

because you can always write an explicit cast (ie you are telling the compiler you know what you are doing and it shouldn't care about possible incompatibilities)

Case 2

If the result of GetData() is something, that isn't castable to Hex the same code

struct Hex { ...} 
...
Hex foo = (Hex)GetData(); 

will still successfully compile (because you are overruling the compiler). But it will throw an InvalidCastException at runtime.

Case 3

Your second snippet won't even compile. Because the result of the as operator is nullable (ie as returns null if the typecheck fails) and you cannot assign null to a non-nullable struct.

struct Hex { ...} 
...
Hex hex = actionData as Hex;

If you want to be able to compile this, you have to make the variable nullable like

Hex? hex = actionData as Hex?;
if (hex.HasValue) {
  ... //conversion succeeded
} else {
  ... //conversion failed and hex is null
}

or you make Hex a class instead of a struct. Because a class is a reference type which can also be null, but a struct is a value type which cannot be null.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!