We can’t directly overload = operator in C#. for doing this we should use implicit operator.
consider the following example
class Student } |
following example demonstrates an implicit conversion operator, so add following method to Student class
public static implicit operator Student(string name) |
Consider another Example ,
Student std1 = new Student();
std1.StudentName = "Crack It";
string studentName = std1;//Error Cannot implicitly convert type 'Student' to 'string'
then we will add another method .
public static implicit operator string(Student std)
{
return std.StudentName;
}
No comments:
Post a Comment