When a variable is converted from one data type to another, it is referred to as Type Casting. We have the flexibility to adjust the data type based on our requirements. In C#, during compilation, the language is statically-typed, meaning once a variable is declared, it cannot be re-declared. The variable's value cannot be directly assigned to a different type of variable without explicitly converting the variable type.
Here we will use a string data type as an example. It's important to note that strings cannot be automatically converted to integers. Hence, if we define a variable 'i' as an integer, we won't be able to directly assign the string value "Hello" to it.
int z;
z = "hii"; // error CS0029: we cannot implicitly convert string type' to 'integer' type
However, a scenario may arise where it becomes necessary to duplicate the value of a variable into another variable or method parameter of a different type. For instance, if we have an integer variable and we require passing it to a method parameter of type double. Alternatively, there may be a need to assign the class variable to a variable of an interface type. These actions are referred to as Type Conversion.
In C#, we can perform a different kinds of conversions.
- Implicit Conversion: For the implicit conversion, there is not any need for the special syntax. This type of conversion is safe; in this conversion, there is not any loss of the data. Implicit conversions include the conversion of the small type to large integral types, and from the derived class to the base class conversion.
- Explicit Conversion (Type Caste): Explicit conversion will be done with the cast operator . We will do the casting when there is the situation of the data loss, or when the conversion is not succeeded. There can be any other reason for the explicit conversion. The example of the casting is the conversion of the numeric type to the less precision or smaller range. Explicit Conversion also includes the conversion of the base-class instance to the derived class.
- User-Defined Conversion: We can do this conversion by defining the method. We can use the technique to enable the explicit-implicit conversion between the custom type, which does not have any relationship with the base-class or derived-class.
For transforming incompatible data types such as integers and System.DateTime objects or hexadecimal strings and byte arrays, the System.BitConversion class, System.Convert class, and the Parse methods of standard numeric types like int32 Parse can be employed.
Understanding and utilizing implicit conversion is straightforward. When assigning an integer to a double, it is considered implicit conversion because no data is lost during this process.
To grasp this transformation, let's delve into an illustration.
Int value1=567;
Int value2=765;
Long sum;
sum=value1+value2
Here we are working with two variables, value1 and value2, which are of the integer data type. We will perform an addition operation on these integer values and store the result in a variable of type long. In this process, no errors will be encountered, and there will be no loss of data. This kind of type conversion is referred to as implicit conversion.
Example:
using System;
namespace ConsoleApp2
{
class SumProgramme
{
static void Main(string[] args)
{
int value1 = 567;
int value2 = 765;
long summation;
summation = value1 + value2;
Console.WriteLine("summation = " + summation);
Console.ReadLine();
}
}
}
Output:
The CSS code below demonstrates the styling for a placeholder diagram:
.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }
Explicit Conversion
We can perform the direct conversion by specifying the method. Individuals will carry out the direct conversion. Users will conduct the conversion based on their specific needs. The compiler will execute the process according to our instructions.
Now we will perform this transformation by implementing the subsequent code snippet:
Example:
using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class ProgramExplicit
{
static void Main(string[] args)
{
double db = 7896.45;
int xy;
// here we do the cast double to int.
xy = (int)db;
Console.WriteLine(xy);
Console.ReadKey();
}
}
}
Output:
Styling for a visual placeholder is defined by the following CSS properties:
- The background includes a linear gradient with specific color stops.
- A border radius of 12 pixels is applied for rounded corners.
- Padding of 40 pixels provides space inside the placeholder.
- Vertical margin of 20 pixels and no horizontal margin is set.
- Text alignment is centered for the content within the placeholder.
- The icon inside the placeholder has a font size of 3rem.
- The text content is styled with a color of #9ca3af and a font size of 1rem.
Conversion Operators
Conversion Operators have the following properties:
- Conversions declared as "implicit" will occur automatically when it is required.
- Conversions declared as "explicit" will require the cast to be called.
- All the conversions must be declared as positive.
Now let's consider a sample that adheres to the provided code snippet.
using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace UserDefinedConversion
{
class Program
{
public struct ImperialMeasurement
{
public float feet;
public ImperialMeasurement(float r)
{
this.feet = r;
}
public static explicit operator ImperialMeasurement(int m)
{
float ConversionResult = 3.28f * m;
ImperialMeasurement temp = new ImperialMeasurement(ConversionResult);
return temp;
}
}
static void Main(string[] args)
{
Console.WriteLine("Please enter a whole number measurement in meters");
int nm = Convert.ToInt32(Console.ReadLine());
ImperialMeasurement im = (ImperialMeasurement)nm;
Console.WriteLine($"The measument of {nm} in meters is {im.feet} in feet ");
Console.ReadKey();
}
}
}
Output: