Type Conversions in C#

C# is a strongly typed language, and that makes mixing data stored in variables of different data types a process that requires thought. For example, look at this code:

int source = 5;
long target;
target = source;
System.Console.WriteLine(target);

Here, we're creating an int variable, source, and a long variable, target, and assigning the value in source to target. Although these variables are not of the same data type, no data will be lost, because this is a widening conversion, where we're moving from a smaller container (int) to a larger one (long). Because data is not lost in a widening conversion, C# has no problem making this conversion, and will do so automatically—this is called implicit conversion.

On the other ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.