All Type Coding

Search Here

Type conversions in c#

 Type conversions

In CSharp type conversions are divided into two category:
1-Implicit Conversions 
Implicit casting doesn't require a casting operator no special syntax is required because the conversion is type safe and no data will be lost.
2-Explicit Conversions
Explicit casting requires a casting operator. Explicit conversions require a cast operator. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. 

Conversion is the process of changing the value of one Type to another. System.Convert class provides a complete set of methods for supported conversions.
Conversions declared as implicit occur automatically, when required and Conversions declared as explicit require a cast to be called.
for Example:
1:int i = 100;
2:long j = i;

  // implicit conversion from int type to long type

Here the conversion occurred automatically. Because we converted an integer type to long type . This type of conversion is called Implicit Conversion .

for Example:
1:int i = 100;
2:long j = i;

  // implicit conversion from int type to long type
3:int i = (int)j;
  // explicit conversion from long type to int type

Here we explicitly convert long type to integer (int i= (int)j), otherwise the compiler will show compiler error - Error : Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?) . This type of conversion is called Explicit Conversion .

The following example shows an explicit type conversion:

Implicit Conversion Example
using System;
class Program
{
    public static void Main()
    {
        int i = 100;

        // float is bigger datatype than int. So, no loss of
        // data and exceptions. Hence implicit conversion
        float f = i;

        Console.WriteLine(f);
    }
}


Explicit Conversion Example
using System;
class Program
{
    public static void Main()
    {
        float f = 100.25F;

        // Cannot implicitly convert float to int.
        // Fractional part will be lost. Float is a
        // bigger datatype than int, so there is
        // also a possiblity of overflow exception
        // int i = f;

        // Use explicit conversion using cast () operator
        int i = (int)f;

        // OR use Convert class
        // int i = Convert.ToInt32(f);

        Console.WriteLine(i);
    }
}


C# Type Conversion Methods

1    ToBoolean
Converts a type to a Boolean value, where possible.

2    ToByte
Converts a type to a byte.

3 ToChar
Converts a type to a single Unicode character, where possible.

4 ToDateTime
Converts a type (integer or string type) to date-time structures.

5 ToDecimal
Converts a floating point or integer type to a decimal type.

6 ToDouble
Converts a type to a double type.

7 ToInt16
Converts a type to a 16-bit integer.

8 ToInt32
Converts a type to a 32-bit integer.

9 ToInt64
Converts a type to a 64-bit integer.

10 ToSbyte
Converts a type to a signed byte type.

11 ToSingle
Converts a type to a small floating point number.

12 ToString
Converts a type to a string.

13 ToType
Converts a type to a specified type.

14 ToUInt16
Converts a type to an unsigned int type.

15 ToUInt32
Converts a type to an unsigned long type.

16 ToUInt64
Converts a type to an unsigned big integer.

1 comment :

  1. Type conversions

    Good educational content about the type conversions in C# students and developers can learn from it...!

    Well, read our blog - Top 6 Practical Reasons to Learn C# Programming Language in 2021

    C# development services

    ReplyDelete