Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int double struct and enum ValueType

Tags:

c#

This question was asked in a technical test recently:

Which of these is a Value Type:

  1. int i
  2. double d
  3. struct S { int a; string b; }
  4. enum Test { a, b, c, d }

I thought that it was a trick question; int and double are structs, and structs are ValueTypes, hence all the answers are Value Types. But only one answer was allowed and apparently the "correct" answer was 4) the enum.

Is there anyway that answer 4) can be the only correct answer?

like image 505
Jason Avatar asked Mar 29 '26 21:03

Jason


2 Answers

Maybe the authors of the questions have a different definition of value types?

All four answers are value types:

int integer = 0;
Console.WriteLine(integer.GetType().IsValueType); //true
double dbl = 0.0;
Console.WriteLine(dbl.GetType().IsValueType); //true
S s = new S();
Console.WriteLine(s.GetType().IsValueType); //true
Test t = Test.a;
Console.WriteLine(t.GetType().IsValueType); //true
like image 104
Bas Avatar answered Mar 31 '26 09:03

Bas


They are all value types. See the value types table on MSDN.

like image 24
Patrick Hofman Avatar answered Mar 31 '26 09:03

Patrick Hofman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!