Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type int? vs type int

Tags:

c#

casting

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?

like image 601
Impostor Avatar asked Mar 27 '19 08:03

Impostor


People also ask

Is it better to use int or integer?

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.

Is Integer a reference type?

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.

What is the difference between an int and an array?

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.

What is the difference between int and integer in Java?

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.

What is the difference between int and word 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.

What is the difference between int and bigint data types?

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.


Video Answer


3 Answers

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.

like image 65
Marc Gravell Avatar answered Oct 17 '22 12:10

Marc Gravell


A nullable value type is boxed by the following rules

  • If HasValue returns false, the null reference is produced.
  • If 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;
like image 20
Johnny Avatar answered Oct 17 '22 10:10

Johnny


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.

like image 4
Farhad Jabiyev Avatar answered Oct 17 '22 10:10

Farhad Jabiyev