-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Casting and Type Conversions in C#
- Implicit/Automatic Type Conversions
- Explicit Conversions
- Type Conversion Methods
Casting and Type Conversions in C#
- C# is statically-typed at compile time, hence after a variable is declared, it cannot be declared again or assigned a value of another type unless that type is implicitly convertible to the variable's type. Sometimes, user might need to copy a value into a variable or method parameter of another type. For example, user might have an integer variable that they need to pass to a method whose parameter is typed as double. These kinds of operations are called type conversions. User can perform the following kinds of conversions in C#:
- Implicit/Automatic Type Conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types (Byte -> short -> int -> long -> float -> double). int myInt = 20; Decimal myDecimal = myInt; // Automatic casting: int to decimal double myDouble = myInt; // Automatic casting: int to double Console.WriteLine(myInt); // Outputs 20 Console.WriteLine(myDecimal); // Outputs 20 Console.WriteLine(myDouble); // Outputs 20
-
Explicit conversions (casts): Explicit conversions require a cast expression. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. Typical examples include numeric conversion to a type that has less precision or a smaller range (double -> float -> long -> int -> short -> Byte). There may be compilation error when types not compatible with each other. double myDecimal1 = 29.789;
//int myInt1 = myDecimal1; // Error CS0266 Cannot implicitly convert type 'double' to 'int'.An explicit conversion exists(are you missing a cast?)
int myInt1 = (int)myDecimal1; // Manual casting: double to int Byte myByte1 = (Byte)myDecimal1; // Manual casting: double to Byte Console.WriteLine(myDecimal1); // Outputs 29.789 Console.WriteLine(myInt1); // Outputs 29 Console.WriteLine(myByte1); // Outputs 29
Type Conversion Methods in C#
- It is also possible to convert data types explicitly by using built-in methods in C#, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long).
int i = 35;
double d = 85.645;
float f = 30.78F;
Console.WriteLine(Convert.ToString(f));
Console.WriteLine(Convert.ToUInt32(f));
Console.WriteLine(Convert.ToInt64(f));
Console.WriteLine(Convert.ToString(d));
Console.WriteLine(Convert.ToUInt32(d));
Console.WriteLine(Convert.ToInt16(d));
Console.WriteLine(Convert.ToInt32(d));
Console.WriteLine(Convert.ToInt64(d));
Console.WriteLine(Convert.ToDouble(i));
Console.WriteLine(Convert.ToDecimal(i));
Method | Description |
---|---|
ToBollen | It will converts a type to Bollen value |
ToChar | It will converts a type to a charecter value |
ToByte | It will converts a value to Byte value |
ToDecimal | It will converts a value to decimal point value |
ToDouble | It will converts a type to double data type |
ToInt16 | It will converts a type to 16-bit integer |
ToInt32 | It will converts a type to 32-bit integer |
ToInt64 | It will converts a type to 64-bit integer |
ToString | It will converts a 54swz given type to String |
ToUlnt16 | It will converts a type to unsigned 16-bit integer |
ToUlnt32 | It will converts a type to unsigned 32-bit integer |
int i = 55;
double d = 78.523;
float f = 89.78F;
Console.WriteLine(Convert.ToUInt16(f));
Console.WriteLine(Convert.ToUInt32(f));
Console.WriteLine(Convert.ToUInt64(f));
Console.WriteLine(Convert.ToUInt16(d));
Console.WriteLine(Convert.ToUInt32(d));
Console.WriteLine(Convert.ToUInt64(d));
Console.WriteLine("*****");
Console.WriteLine(Convert.ToInt16(f));
Console.WriteLine(Convert.ToInt32(f));
Console.WriteLine(Convert.ToInt64(f));
Console.WriteLine(Convert.ToInt16(d));
Console.WriteLine(Convert.ToInt32(d));
Console.WriteLine(Convert.ToInt64(d));
Console.WriteLine(Convert.ToString(f));
Console.WriteLine(Convert.ToString(d));
Console.WriteLine(Convert.ToString(i));
double myDouble1 = 56.123;
int myInt2 = (int)myDouble1;
Byte myByte1 = (Byte)myDouble1;