→Casting Variable Types in C#
The same is not true of assigning a ''long'' to an ''int'' since a long is capable of storing significantly larger numbers than an ''int'' (an ''int'' is 4 bytes long versus 8 bytes for a ''long''). Attempting to squeeze a ''long'' into an ''int'' value would inevitably result in lost data. For this reason the C# compiler will flag such an attempt as an error and stubbornly refuse to compile the code.
Casts are performed by placing the variable type to which you wish to convert before the name of the variable you wish to convert from. For example, to convert a ''long'' value to an ''int'' during an assignment operation:
Note that casting is only possible on numerical data types. It is not, therefore, possible to perform casts on ''string'', or ''bool'' variables.