|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn some situations, you will want to use an object of a class in an expression involving other types of data. Sometimes, overloading one or more operators can provide the means of doing this. However, in other cases, what you want is a simple type conversion from the class type to the target type. To handle these cases, C# allows you to create a special type of operator method called a conversion operator. A conversion operator converts an object of your class into another type. In essence, a conversion operator overloads the cast operator. Conversion operators help to fully integrate class types into the C# programming environment by allowing objects of a class to be freely mixed with other data types, as long as a conversion to those other types is defined. There are two forms of conversion operators, implicit and explicit. The general form for each is shown here: public static operator implicit target-type(source-type v)
{
return value;
}
public static operator explicit target-type(source-type v)
{
return value;
}
Here, Using the codeTo illustrate a conversion operator, we will create one for the public static implicit operator Student(string str)
{
return new Student(str);
}
Here is a program that illustrates this conversion operator: // An example that uses an implicit conversion operator.
using System;
using System.Collections.Generic;
using System.Text;
namespace operatoreOverload
{
class Student
{
private string FullName = "";
public Student(string str)
{
FullName = str;
}
public static implicit operator Student(string str)
{
return new Student(str);
}
public static implicit operator string(Student stu)
{
return stu.FullName;
}
}
class Program
{
static void Main(string[] args)
{
Student stud="Implicit define";
string str = stud;
Console.WriteLine(str);
Console.ReadKey();
}
}
}
This program displays the output: Implicit define
As the program illustrates, when we use a string in an object experssion such as public static implicit operator string(Student stu)
{
return stu.FullName;
}
Alternatively, you can create an explicit conversion operator that is invoked only when an explicit cast is used. An explicit conversion operator is not invoked automatically. For example, here is the previous program reworked to use an explicit conversion to the // An example that uses an implicit conversion operator.
using System;
using System.Collections.Generic;
using System.Text;
namespace operatoreOverload
{
class Student
{
private string FullName = "";
public Student(string str)
{
FullName = str;
}
public static explicit operator Student(string str)
{
return new Student(str);
}
public static explicit operator string(Student stu)
{
return stu.FullName;
}
}
class Program
{
static void Main(string[] args)
{
Student stud=(Student)"Implicit define";
string str = (string)stud;
Console.WriteLine(str);
Console.ReadKey();
}
}
}
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||