Jump to: navigation, search

C Sharp Variables and Constants

18 bytes removed, 16:39, 14 January 2008
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.
it It is just possible , however, that you as the programmer, know that even though a variable is a ''long'' type that it will never contain a value greater than an ''int'' is capable of storing. In this case you might legitimately want to assign the value stored in a ''long'' variable to an ''int'' variable. This can be achieved by using a ''cast '' to convert the long value.
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.

Navigation menu