hosting tips

ASP.NET Tutorial: Implicit Operator in C#

In C#, the implicit operator approach allows you to define implicit conversion between two types. It enables you to convert an instance of one type to another type implicitly without requiring an explicit casting operator.

To define an implicit operator in C#, you need to use the implicit keyword followed by the target type. Here’s the general syntax:

public static implicit operator TargetType(SourceType source)
{
    // Conversion logic here
    // Return an instance of TargetType
}

Let’s say you have two custom classes, SourceType and TargetType, and you want to enable implicit conversion from SourceType to TargetType.

The source code can be downloaded from GitHub

For this tutorial, I have used the below tools.

  1. Visual Studio 2022 Community Edition
  2. .NET 6.0
  3. Console App

Here is an example:

Let’s create two classes, ContractEmployee and PermanentEmployee, and demonstrate how to use an implicit operator to convert from ContractEmployee to PermanentEmployee.

internal class ContractEmployee
{
    public string Name { get; set; }
    public decimal HourlyRate { get; set; }

    public ContractEmployee(string name, decimal hourlyRate)
    {
        Name = name;
        HourlyRate = hourlyRate;
    }
}

internal class PermanentEmployee
{
    public string Name { get; set; }
    public decimal MonthlySalary { get; set; }

    public PermanentEmployee(string name, decimal monthlySalary)
    {
        Name = name;
        MonthlySalary = monthlySalary;
    }
    /// <summary>
    /// Implicit Conversion
    /// </summary>
    /// <param name="contractEmployee"></param>
    public static implicit operator PermanentEmployee(ContractEmployee contractEmployee)
    {
        // Convert ContractEmployee to PermanentEmployee
        decimal monthlySalary = contractEmployee.HourlyRate * 160; // Assuming 160 working hours in a month
        return new PermanentEmployee(contractEmployee.Name, monthlySalary);
    }
}

In the example above, we have two classes: ContractEmployee and PermanentEmployee. The ContractEmployee class represents an employee on a contract with an hourly rate, while the PermanentEmployee class represents an employee on a permanent basis with a monthly salary.

The PermanentEmployee class defines an implicit operator that converts a ContractEmployee instance to a PermanentEmployee. In this conversion, we assume that a contract employee works for 160 hours in a month, and we calculate the equivalent monthly salary based on their hourly rate.

Now, let’s see how to use the implicit conversion:

using ImplcitOperatorTutorial;

Console.WriteLine("Implict Operator Example");
ContractEmployee contractEmployee = new ContractEmployee("Micheal Joe", 25); // Hourly rate: $25

PermanentEmployee permanentEmployee = contractEmployee; // Implicit conversion happens here

Console.WriteLine(permanentEmployee.Name); // Output: Michael Joe
Console.WriteLine(permanentEmployee.MonthlySalary); // Output: 4000 (25 * 160)
Console.ReadLine();

In the code snippet above, we create a ContractEmployee object named contractEmployee with the name of “John Doe” and an hourly rate of $25. Then, we assign the contractEmployee to a PermanentEmployee variable named permanentEmployee. The implicit operator defined in the PermanentEmployee class handles the conversion.

Finally, we print the Name and MonthlySalary properties of the permanentEmployee, which now holds the converted values. The output shows the name as “Mochael Joe” and the calculated monthly salary as $4000 based on the assumed 160 working hours in a month and the hourly rate of $25.

This is just a simple example to illustrate the concept of using an implicit operator for conversion between different types. In real-world scenarios, you would likely have more complex conversion logic based on your requirements.

ASP.NET Core 8 Hosting Recommendation

HostForLIFE.eu

HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.