Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

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
Source by www.sean-lloyd.com #
 
PREVIOUS NEXT
Tagged: #implicit #explicit #cast
ADD COMMENT
Topic
Name
2+7 =