Answers for "implicit vs explicit cast c#"

C#
0

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
Posted by: Guest on May-17-2021

C# Answers by Framework

Browse Popular Code Answers by Language