C#/C# Tutorial

C# Type Casting | C# Tutorial for Beginners

DragonTory 2023. 2. 14. 20:51
반응형

 C# Type Casting 

 

C# type casting is the process of converting a value of one data type to another data type. 

There are two types of type casting in C#: 

implicit casting and explicit casting.

Implicit casting, also known as widening conversion, is the automatic conversion of a value of a smaller data type to a larger data type.

 For example, converting an int to a long.

 Implicit casting does not require any special syntax.

Explicit casting, also known as narrowing conversion, is the manual conversion of a value of a larger data type to a smaller data type. 

For example, converting a double to an int. 

Explicit casting requires the use of a cast operator and may result in a loss of data.



Here's an example of implicit casting in C#:

1
2
3
int num1 = 5;
long num2 = num1; // implicit casting
 
cs

 

Here's an example of explicit casting in C#:

1
2
3
double num1 = 5.6;
int num2 = (int)num1; // explicit casting
 
cs

 

 C# Type Conversion Methods 

C# provides several casting methods to convert values from one type to another. 

These methods are defined in the System namespace and are as follows:

  • Convert.ToByte(): Converts a value to a byte.
  • Convert.ToInt16(): Converts a value to a 16-bit integer.
  • Convert.ToInt32(): Converts a value to a 32-bit integer.
  • Convert.ToInt64(): Converts a value to a 64-bit integer.
  • Convert.ToSingle(): Converts a value to a single-precision floating-point number.
  • Convert.ToDouble(): Converts a value to a double-precision floating-point number.
  • Convert.ToDecimal(): Converts a value to a decimal number.
  • Convert.ToString(): Converts a value to a string.

Here's an example of using the Convert.ToInt32() method to convert a string to an integer:

1
2
3
string strNum = "123";
int intNum = Convert.ToInt32(strNum);
 
cs

In this example, the string "123" is converted to an integer using the Convert.ToInt32() method. 

The resulting value is assigned to the variable intNum. 

Note that the method throws an exception if the input string cannot be converted to an integer.

 

You can convert an integer to a string using the ToString() method.

This method is defined for all numeric types, including int.

int num = 42;
string strNum = num.ToString();

In this example, the integer value 42 is converted to a string using the ToString() method. 

The resulting value is assigned to the variable strNum.

 

 C# Type Conversion using Parse() 

C# provides type conversion methods to convert values from one type to another.

These methods are provided by the System namespace and are as follows:

  • bool.Parse(): Converts a string representation of a boolean value to its boolean equivalent.
  • int.Parse(): Converts a string representation of a number to its integer equivalent.
  • double.Parse(): Converts a string representation of a number to its double-precision floating-point equivalent.
  • float.Parse(): Converts a string representation of a number to its single-precision floating-point equivalent.
  • decimal.Parse(): Converts a string representation of a number to its decimal equivalent.
  • bool.TryParse(): Tries to convert a string representation of a boolean value to its boolean equivalent and returns a boolean value that indicates whether the conversion succeeded.
  • int.TryParse(): Tries to convert a string representation of a number to its integer equivalent and returns a boolean value that indicates whether the conversion succeeded.
  • double.TryParse(): Tries to convert a string representation of a number to its double-precision floating-point equivalent and returns a boolean value that indicates whether the conversion succeeded.
  • float.TryParse(): Tries to convert a string representation of a number to its single-precision floating-point equivalent and returns a boolean value that indicates whether the conversion succeeded.
  • decimal.TryParse(): Tries to convert a string representation of a number to its decimal equivalent and returns a boolean value that indicates whether the conversion succeeded.

 

Here's an example of using the int.Parse() method to convert a string to an integer:

1
2
3
string strNum = "123";
int intNum = int.Parse(strNum);
 
cs

 

In this example, the string "123" is converted to an integer using the int.Parse() method. 

The resulting value is assigned to the variable intNum. 

Note that the method throws an exception if the input string cannot be converted to an integer.

반응형