implicit vs explicit c# code example

Example: implicit vs explicit cast c#

// Implicit (converting from a smaller type to a larger type)
int anInt = 12;
float aFloat = anInt;

Console.WriteLine($"anInt: {anInt}"); // anInt: 12
Console.WriteLine($"aFloat: {aFloat}"); // aFloat: 12

// Explicit (converting from a larger type to a smaller type)
float aFloat = 12.945f;
int anInt = (int) aFloat;

Console.WriteLine($"aFloat: {aFloat}"); // aFloat: 12.945
Console.WriteLine($"anInt: {anInt}"); // anInt: 12