I've this comparison which equals false
as expected
bool eq = typeof(int?).Equals(typeof(int));
now I have this code
List<object> items = new List<object>() { (int?)123 };
int result = items.OfType<int>().FirstOrDefault();
but this returns 123
- anyway that value is of type int?
How can this be?
int provides less flexibility as compare to Integer as it only allows binary value of an integer in it. Integer on other hand is more flexible in storing and manipulating an int data. Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics.
Int is certainly not a reference type in C#. It's a numerical struct, which is a value type. When talking about C#, it is incorrect to say int is a reference type.
Consider the type int, which is typical of value types. A value of type int is completely self-contained; all the bits needed to represent that value are stored in that value, and every bit pattern in that value represents a valid value for its type. Now, consider the array type int [], which is typical of reference types.
So, the main difference between the int and Integer is that the int is of primitive data type while the Integer is of class type. In the development of the OOPs application, int behaves according to the principle of the primitive data type.
I am not sure if it applies to Siemens platforms. data type INT always represent the full bit length of the CPU, so it may vary at different CPUs. On an Intel 32-bit processor an INT is 32 bit long. data type WORD is always 2 Byte, that is 16 bit long.
The int data type is the primary integer data type in SQL Server. The bigint data type is intended for use when integer values might exceed the range that is supported by the int data type.
Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object
, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null
(regular null
, no type), or as the non-nullable type (the T
in T?
). So: an int?
is boxed as an int
, not an int?
. Then when you use OfType<int>()
on it, you get all the values that are int
, which is: the single value you passed in, since it is of type int
.
A nullable value type is boxed by the following rules
HasValue
returns false
, the null reference is produced. HasValue
returns true
, a value of the underlying value type T
is
boxed, not the instance of nullable.In your example second rule has been followed as you have value:
var i = (object)(int?)123;
It is a bit late, but beside of Marc's answer to your question, I want to give some additional information about Nullable value types in CLR.
The CLR has built-in support for nullable value types. This special support is provided for boxing, unboxing, calling GetType
, calling interface methods.
For example, let's check GetType()
:
Int32? x = 5;
Console.WriteLine(x.GetType());
What you think it will print to the console?
System.Nullable<Int32
? Not, the result is System.Int32
.
Or, let's check boxing, which you noted in your question:
Int32? n =5;
Object o = n;
Console.WriteLine("o's type={0}", o.GetType()); // "System.Int32"
The rule is that:
When the CLR is boxing a Nullable instance, it checks to see if it is null, and if so, the CLR doesn’t actually box anything, and null is returned. If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words, a Nullable with a value of 5 is boxed into a boxed-Int32 with a value of 5.
And, at the end I want to explain how CLR add special support for calling interface methods from Nullable Types. Let's take a look to that:
Int32? n = 5;
Int32 result = ((IComparable) n).CompareTo(5); // Compiles & runs OK
Console.WriteLine(result); // 0
In the preceding code, I’m casting n, a Nullable<Int32>
, to IComparable<Int32>
, an interface
type. However, the Nullable<T>
type does not implement the IComparable<Int32>
interface as
Int32
does. The C# compiler allows this code to compile anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With