65.9K
CodeProject 正在变化。 阅读更多。
Home

转换运算符

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.43/5 (11投票s)

2007年5月25日

CPOL

2分钟阅读

viewsIcon

42351

downloadIcon

3899

转换运算符将您的类对象转换为另一种类型。

引言

在某些情况下,您希望在涉及其他类型数据的表达式中使用类的对象。有时,重载一个或多个运算符可以提供实现此目的的方法。但是,在其他情况下,您想要的是从类类型到目标类型的简单类型转换。为了处理这些情况,C# 允许您创建一种特殊的运算符方法,称为转换运算符。转换运算符将类的对象转换为另一种类型。本质上,转换运算符重载了强制类型转换运算符。转换运算符通过允许类的对象与其它数据类型自由混合,只要定义了转换为这些其它类型的转换,来帮助将类类型完全集成到 C# 编程环境中。转换运算符有两种形式:隐式和显式。每种形式的一般形式如下所示

public static operator implicit target-type(source-type v) 
{ 
    return value; 
}

public static operator explicit target-type(source-type v) 
{ 
    return value; 
}

这里,target-type 是您正在转换的目标类型,source-type 是您正在转换的类型,而 value 是转换后的类的值。转换运算符返回 target-type 类型的数据,不允许其他返回类型说明符。如果转换运算符指定 implicit,则在对象用于与目标类型一起的表达式中时,将自动调用该转换。当转换运算符指定 explicit 时,在使用了强制类型转换时调用该转换。您不能为相同的目标类型和源类型定义隐式和显式转换运算符。

使用代码

为了说明转换运算符,我们将为 Student 类创建一个。假设您想将字符串转换为对象类型。为了完成此操作,您将使用一个隐式转换运算符,其形式如下

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

这是一个说明此转换运算符的程序

// 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(); 
            } 
        } 
    }     

该程序显示以下输出

Implicit define

如程序所示,当我们使用字符串在对象表达式中,例如 Student stud="Implict define" 时,将对字符串应用转换。在这种特定情况下,转换返回一个将 Fullname 字段设置为的 对象。但是,当表达式不需要转换为 Student 对象时,不会调用转换运算符。请记住,在前面的示例中,我们通过以下代码将 Student 对象转换为字符串

public static implicit operator string(Student stu) 
{
    return stu.FullName; 
} 

或者,您可以创建一个显式转换运算符,该运算符仅在使用了显式强制类型转换时才被调用。显式转换运算符不会自动调用。例如,这是先前程序重新编写以使用显式转换到 Student 对象和字符串

// 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(); 
            } 
        } 
    }

历史

  • 2007 年 5 月 25 日 - 发布原始版本
© . All rights reserved.