Type Conversion and Casting in c#

Type Conversion and Casting in c#

In the C# programming language, data type conversion means converting a user-defined, declared numeric or text variable parameter value from one data type to another. There are two main types of data type parameter value conversions in C# programming: implicit and explicit.

Type Conversion and Casting in c#

So, let’s explore the data type conversion and casting methods in C# programming.

Implicit data type conversion/type promotion in C#.

Implicit data type conversion is performed automatically in the C# programming language when a user-defined program updates or converts a declared data type value from a small data type to a large data type. There is no risk of data loss or data overflow.

Examples of implicit data conversion in the C# programming language.

  • Updating or converting an existing data value from a small to a large integral data type, including from an int to a long data type.
  • Converting or updating a user-declared integer data type parameter value from an int to a float or double data type as a floating point.

Example of implicit conversion.

int numeric = 77;

long bigNumeric = numeric; // Here, implicit data type conversion is performed without data loss during the conversion process.

float decimalnumeric = numeric; // This converts the int data type into the float data type with the fractional value 77.0f.

Explicit Data Type Conversion/Casting in C#.

In the C# programming language, explicit data casting or conversion is performed when there is a risk of data loss or conversion from a larger data type to a smaller data type in a user-defined data type variable parameter value.

Explicit Data Type Conversion Syntax.

targetType variableName = (targetType)sourceValue;

Explicit Data Type Example:

double numeric = 13.38;

int intNumeric = (int)numeric; // Here we perform explicit data casting, it automatically truncates the decimal part.

Console.WriteLine(intNumeric); // Result is – 13

Remember: Casting a variable parameter from a larger data type to a smaller data type can result in loss of the actual data type value or overflow of the data value.

Convert using built-in methods in the C# programming language.

The C# programming language provides the Convert class method for secure and controlled data type conversion.

Example of Convert using built-in methods.

string decimaltoString = “874”;

int output = Convert.ToInt32(decimaltoString); // Here it converts the string data type to an int data type.

Console.WriteLine(output); // Result is – 874

Popular C# program data type conversion method.

  • Convert.ToInt32()
  • Convert.ToDouble()
  • Convert.ToBoolean()
  • Convert.ToString()

Parsing string data type to numeric data type in a C# program.

In the C# programming language, the int.Parse() or double.Parse() built-in C# function methods are used to convert string data type text values ​​to number data types.

Example of parsing string data type to numeric data type.

string values ​​= “982”;

int parsedValue = int.Parse(values); // This converts the string value into an integer data type value.

Console.WriteLine(parsedValue); // The result is – 982

Remember: When converting a string data type to a valid number data type, you must have a valid number. Otherwise, it will throw a program exception error.

Use the TryParse method for safe conversion in a C# program.

In the C# programming language, the TryParse() function method throws an exception and indicates whether the data type conversion was successful.

Example of the TryParse method for safe conversion.

string values ​​= “148”;

bool succevent = int.TryParse(values, out int numeric);

if (succevent)

{

Console.WriteLine($”it Parsed the numeric value – {numeric}”);

}

else

{

Console.WriteLine(“Invalid user values ​​entered.”);

}

Detail explanation of data Type Conversion and Casting in c#.

Data Conversion TypeMethod to useData type conversion Example
Implicit data type conversionIt performs Automatic in from small data type to large data type methodint -> long data type conversion
Explicit (Casting) data type conversionUse existing c# data (type) operator or method to convert(int)11.24 it converts int data type
Convert ClassUse convert class to Convert.ToInt32 to desire data type conversion methodConvert.ToDouble(“7.3”) it converts double data type
Parse method functionUse this method int.Parse() to convert integer valueint.Parse(“984”) it converts integer numeric values
TryParse methodint.TryParse() use this method function for integer conversionint.TryParse(“492”, out p)

C# program boxing and unboxing conversion.

  • Boxing conversion – This converts a user-defined data type value type to an object.
  • Unboxing conversion – This extracts a user-defined data type value type from an object to a value type.

Example of boxing and unboxing conversion.

int integer = 87;

object boxedInteger = integer; // here it converts object into the Boxing method

int unboxedInteger = (int)boxedInteger; // here it converts object into the Unboxing method

Leave a Reply