What are implicit type conversions in C#?

CsharpProgrammingServer Side Programming

Type conversions in C# has both implicit as well as explicit type conversions. Under Implicit, the conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.

To understand the concept, let us implicitly convert int to long −

int val1 = 11000;
int val2 = 35600;
long sum;

sum = val1 + val2;

Above, we have two integer variable and when we sum it in a long variable, it won’t shown an error. Since the compiler does implicit conversion on its own.

Let us print the values now −

Example

 Live Demo

using System;
using System.IO;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         int val1 =11000;
         int val2 =35600;
         long sum;

         sum = val1 + val2;

         Console.WriteLine("Sum= " + sum);

         Console.ReadLine();
      }
   }
}

Output

Sum= 46600
raja
Published on 10-Aug-2018 12:53:55
Advertisements