欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 中使用隐式和显式操作符的示例

程序员文章站 2022-07-06 17:13:04
c# 有一个鲜为人知的特性是通过定义 显式和隐式操作符 实现类型之间的转换,这篇文章我们将会讨论如何使用这些 显式 和 隐式 操作符。什么是显式,什么是隐式隐式类型转换 它是运行时自动帮你完成的,言外...

c# 有一个鲜为人知的特性是通过定义 显式和隐式操作符 实现类型之间的转换,这篇文章我们将会讨论如何使用这些 显式 和 隐式 操作符。

什么是显式,什么是隐式

隐式类型转换 它是运行时自动帮你完成的,言外之意就是你不需要人为干预,比如下面的例子就是典型的 隐式类型转换。

int x = 100; 
double d = x;

不过下面的代码则过不了编译器。

double d = 100.25;
int x = d;

编译程序时,将会出现下面的错误。

C# 中使用隐式和显式操作符的示例

显而易见,上面的 double 不能隐式的转成 int,除非显式转换,那如何显式呢?可以使用如下代码。

int x = 100; 
double d = (int) x;

人工干预后,编译器也就放行了。

创建 dto 类

接下来我们研究一下如何在 用户自定义类型 上使用 隐式 和 显式转换,比如:class,考虑下面的类。

    public class author
    {
        public guid id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
    }

    public class authordto
    {
        public string id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
    }

在上面的代码中,定义了一个 author 实体类,然后再为 author 定义一个数据传输对象 authordto,数据传输对象是一个数据容器,常用于在 presentation 和 application层 之间传递数据。

model 和 dto 之间的相互转换

下面的代码展示了如何实现 author 和 authordto 之间的相互转换。

        public authordto convertauthortoauthordto(author author)
        {
            authordto authordto = new authordto
            {
                id = author.id.tostring(),
                firstname = author.firstname,
                lastname = author.lastname
            };
            return authordto;
        }

        public author convertauthordtotoauthor(authordto authordto)
        {
            author author = new author
            {
                id = guid.parse(authordto.id),
                firstname = authordto.firstname,
                lastname = authordto.lastname
            };
            return author;
        }

如果需要在应用程序中为若干个类写这样的转换代码,你会发现实现类之间的转换使的代码比较冗余,而且代码可读性也好不到哪里去。所以在这种场景下就是 显式 和 隐式 操作符的用武之地。

使用隐式操作符

实现 model-dto 之间的转换更简单粗暴的方式就是使用 隐显式操作符,这样就避免了冗长的方法调用,让代码更加的直截了当。

下面的代码展示了如何使用 隐式操作符 将 author实例 转成 authordto 实例。

public static implicit operator authordto(author author)
{
  authordto authordto = new authordto();
  authordto.id = author.id.tostring();
  authordto.firstname = author.firstname;
  authordto.lastname = author.lastname;
  return authordto;
}

接下来看一下如何在 main 方法中使用 隐式操作符。

static void main(string[] args)
{
   author author = new author();
   author.id = guid.newguid();
   author.firstname = "joydip";
   author.lastname = "kanjilal";
   authordto authordto = author;
   console.readkey();
}

使用显式操作符

下面的代码展示了如何利用 显式操作符 将 author 实例转成 authordto 。

public static explicit operator authordto(author author)
{
  authordto authordto = new authordto();
  authordto.id = author.id.tostring();
  authordto.firstname = author.firstname;
  authordto.lastname = author.lastname;
  return authordto;
}

这时候在 main 方法中就需要人工介入进行强转了,如下代码所示:

static void main(string[] args)
{
  author author = new author();
  author.id = guid.newguid();
  author.firstname = "joydip";
  author.lastname = "kanjilal";
  authordto authordto = (authordto)author;
  console.readkey();
}

值得注意的是,你不能在一个类中的对象转换同时定义 显式 和 隐式操作符,如下图所示:

C# 中使用隐式和显式操作符的示例

如果你定义了隐式操作符,那么对象之间的转换可以是隐式或显式,如果你定义了显式操作符,那么你只能显式的实现对象转换,虽然隐式操作使用起来非常方便,但显式操作会让代码意图更明显,可读性更高。

以上就是c# 中使用隐式和显式操作符的示例的详细内容,更多关于c# 中使用隐式和显式操作符的资料请关注其它相关文章!