This question already has an answer here:
- What exactly is a type cast in C/C++? 4 answers
decimal d = 2;
int i = (int) d;
I've seen this several times in which parentheses are wrapped around data types.
Why not just use int i = int d;
?
This question already has an answer here:
decimal d = 2;
int i = (int) d;
I've seen this several times in which parentheses are wrapped around data types.
Why not just use int i = int d;
?
The usage of
(int)
is calledcast
ing (or, type-casting). It is essentially telling that,interpretconvert the value ofd
asto anint
(integer) and store it intoi
.In other words, it is a way of converting a type to another one (subject to validity of the conversion).
BTW,
int i = int d;
, as is, is not a valid statement.You're casting d to type int from type decimal. This happens in other languages as well that use static typing.
It is a way of converting data type from one type to another using cast operator, usage is as follows: