神刀安全网

Type Conversions in C#

In this article, we will discuss:

  1. What are conversions in C# ?
  2. Different type of Conversions in C#
  3. Implicit Conversion
  4. Explicit Conversion
  5. User defined Conversion
  6. Summary

What are conversions in C# ?

Conversion is the mechanism of converting a value of one type to the equivalent value of another type.

Type Conversions in C#

Let’s look at below example to understand it better.

Type Conversions in C#

1. var1 is of type short, a 16-bit signed integer that is initialized to 5. var2 is of type sbyte, an 8-bit signed integer that is initialized to the value 10.

2. Then the value of var1 is assigned to var2. Since these are two different types, the value of var1 must be converted to a value of the same type as var2 before the assignment can be performed.

3. If you will look at the output, the value of var1 is unchanged and var2 has same value as var1.

Different type of Conversions in C#

We will discuss, different types of conversions in C#.

  1. Implicit Conversion
  2. Explicit Conversion
  3. User Defined Conversion

Implicit Conversion :

  • The compiler does these conversions automatically
  • When converting from a smaller data type to a larger type, the extra most significant bits of the target are filled with 0s.
  • No data will be lost in Implicit Conversion

Example:

class Program     {       static void Main(string[] args)         {           int  a = 5;             Console.WriteLine(a);             // Implicit conversion from a to b.             double b = a;             Console.WriteLine(b);         }     } 

Output:

5

5

Explicit Conversion :

  • When you convert from a shorter type to a longer type, it’s easy for the longer type to hold all the bits of the shorter type but when you convert from longer type to shorter type, the target type might not be able to accommodate the source value without loss of data.
  • This results in an overflow and loss of data

Example:

 class Program     {       static void Main(string[] args)         {           double  a = 5;             Console.WriteLine(a);             // Implicit conversion from a to b.             int b = (int) a;             Console.WriteLine(b);         }     } 

Output:

55

In the above example, when we will try to convert double to int, we need to explicitly cast using int else we will get an error.

User Defined Conversion:

  • you can also define both implicit and explicit conversions for your own classes and structs.
  • The syntax is the same for both implicit and explicit conversion declarations,except for the keywords implicit and explicit.
  • Both the public and static modifiers are required

Example:

 class Person     {         public string Name;         public int Age;         public Person(string name, int age)         {             Name = name;             Age = age;         }         public static implicitoperatorint(Person p) // Convert Person to int.         {             return p.Age;         }         public static implicitoperatorPerson(int i) // Convert int to Person.         {             return new Person("Tina", i); // ("Tina" is Latin for "No one".)         }     }   class Program   {       static void Main()       {           PersonPerson1 = new Person("Mike", 25);           //Convert a Person object to an int.           int age = Person1;           Console.WriteLine("Person Info: {0}, {1}", Person1.Name, age);           //Convert an int to a Person object           PersonPerson2 = 35;           Console.WriteLine("Person Info: {0}, {1}", Person2.Name, Person2.Age);       }   } 

Output:

Person Info: Mike, 25

Person Info: Tina, 35

Constraints on User defined Conversion:

  • You can only define user-defined conversions for classes and structs.
  • You cannot redefine standard implicit or explicit conversions.
  • The following are true for source type S and target type T:
    − S and T must be different types.
    − S and T cannot be related by inheritance. That is, S cannot be derived from T,and T cannot be derived from S.
    − Neither S nor T can be an interface type or the type object.
    − The conversion operator must be a member of either S or T.
  • You cannot declare two conversions, one implicit and the other explicit, with the same source and target types.

Summary:

In this article, we have discussed:

  • What are conversions in C# ?
  • Different type of Conversions in C#
  • Implicit Conversion
  • Explicit Conversion
  • User defined Conversion
  • Casting in C#

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Type Conversions in C#

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址