In C #, you can not normally convert or cast classes into an int variable or any other variable, and vice versa. By default, you cannot assign an int number to a class. But you can do this by using implicit operators and explicit operators. How the implicit operator works is that you want to cast a type to your class. To do this, you need to create an implicit operator in your desired class to specify that you can cast a specific type to your desired class.

public class Student
{
    public Student(string fullName)
    {
        FullName = fullName;
    }
    public string FullName { get; set; }
    public int Age { get; set; }

    public static implicit operator Student(string name)
    {
        return new Student(name);
    }
}

In the above code we have created a class called Student which contains an implicit operator for the string type. By doing this we can assign a string variable to the Student class as follows:

class Program
{
    static void Main(string[] args)
    {
        Student student = "Farhad Zamani";
        Console.WriteLine(student.FullName);
    }
}

This will create a new Student object whose FullName property is equal to the value of the string variable we gave it when we put the value of a string variable inside the Student class.

But if we want to do the opposite operation, we have to use the implicit operator again. That means we need to create an implicit operator that converts the Student class to a string type or any other type we need. To do this, we must do the following:

public class Student
{
    public Student(string fullName)
    {
        FullName = fullName;
    }
    public Student(string fullName, int age) : this(fullName)
    {
        Age = age;
    }

    public string FullName { get; set; }
    public int Age { get; set; }

    public static implicit operator Student(string name)
    {
        return new Student(name);
    }

    public static implicit operator int(Student student)
    {
        return student.Age;
    }
}

By doing this we can assign the Student class to an int variable and the value of the Age property is set to the variable to which it is assigned:

static void Main(string[] args)
{
    Student student = "Farhad Zamani";
    student.Age = 21;
    int age = student;
    Console.WriteLine(student.FullName + "\t" + age);
}

But if we want the developer to do the cast operation, we can use the explicit operator. The difference between explicit and implicit is that in explicit we must do the casting operation, otherwise the compiler will show the following error:

Cannot implicitly convert type '' to ''. An explicit conversion exists (are you missing a cast?)

If we want to use explicit to assign the Student class to the int variable, we must do the following:

public static explicit operator int(Student student)
{
    return student.Age;
}

Cast operation:

static void Main(string[] args)
{
    Student student = "Farhad Zamani";
    student.Age = 21;
    int age = (int)student;//<--NOTE THIS
    Console.WriteLine(student.FullName + "\t" + age);
}

;)

Powered by Froala Editor

Comments