explicit 和 implicit 的用法
explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换
explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A)
implicit 表示隐式转换,如从 B -> A 只需直接赋值(A = B)
隐式转换可以让我们的代码看上去更漂亮、更简洁易懂,所以最好多使用 implicit 运算符。不过!如果对象本身在转换时会损失一些信息(如精度),那么我们只能使用 explicit 运算符,以便在编译期就能警告客户调用端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace implicit_explicit
{
class Program
{
static void Main(string[] args)
{
TesImplicit t = new TesImplicit();
User u = new User() { Id = , Name = "lichaoqiang" };
Console.WriteLine("隐式转换-----------------------------------------------------------------");
Customer c = u;
Console.WriteLine(c.CustomerId+"="+c.CustomerName); #region 2>显示转换
Customer customer = u;
VIP v = (VIP)customer;
Console.WriteLine("显示转换-----------------------------------------------------------------");
t.GetVIPInfo(v);
#endregion Console.ReadLine();
}
} /// <summary>
/// test case
/// </summary>
public class TesImplicit
{
/// <summary>
/// get the customer infomation
/// </summary>
/// <param name="customer"></param>
public void GetCusterInfo(Customer customer)
{
Console.WriteLine("Customer:" + customer.CustomerName);
} public void GetVIPInfo(VIP vip)
{
Console.WriteLine("VIP:" + vip.VName);
}
}
/// <summary>
///
/// </summary>
public class User
{
/// <summary>
///
/// </summary>
public long Id { get; set; } /// <summary>
///
/// </summary>
public string Name { get; set; } /// <summary>
/// 隐式转换
/// </summary>
/// <param name="u"></param>
/// <returns></returns>
public static implicit operator Customer(User u)
{
return new Customer()
{
CustomerId = u.Id,
CustomerName = u.Name
};
} } /// <summary>
///
/// </summary>
public class Customer
{
/// <summary>
///
/// </summary>
public long CustomerId { get; set; } /// <summary>
///
/// </summary>
public string CustomerName { get; set; } /// <summary>
/// 显示转换
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static explicit operator VIP(Customer c)
{
return new VIP()
{
VID = c.CustomerId,
VName = c.CustomerName
};
}
} /// <summary>
///
/// </summary>
public class VIP
{
/// <summary>
///
/// </summary>
public long VID { get; set; } /// <summary>
///
/// </summary>
public string VName { get; set; }
}
}
explicit 和 implicit 的用法的更多相关文章
- C#中转换运算符explicit、implicit、operator、volatile研究
C#中的这个几个关键字:explicit.implicit与operator,估计好多人的用不上,什么情况,这是什么?字面解释:explicit:清楚明白的;易于理解的;(说话)清晰的,明确的;直言的 ...
- 【RS】CoupledCF: Learning Explicit and Implicit User-item Couplings in Recommendation for Deep Collaborative Filtering-CoupledCF:在推荐系统深度协作过滤中学习显式和隐式的用户物品耦合
[论文标题]CoupledCF: Learning Explicit and Implicit User-item Couplings in Recommendation for Deep Colla ...
- operator、explicit与implicit
说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...
- C#的关键字Explicit 和 Implicit
一.explicit和implicit explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符:implicit 关键字用于声明隐式的用户自定义的类型转换运算符. 总结来说: ...
- C#中的explicit和implicit了解一下吧
今天在研究公司项目框架的时候看到了下面的用法,public static implicit operator JsonData(int data);.貌似很久没用过这种隐式转换的写法了,因此重新温习一 ...
- (一) operator、explicit与implicit 操作符重载
原文地址: Click Here 操作符重载必须用public static 应为操作符是用来操作实例的. operator operator ...
- 可空类型(Nullable<T>)及其引出的关于explicit、implicit的使用
问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int&g ...
- C# explicit与implicit
1.它们解决什么问题? 考虑下面的需求,Person类有个字段age.我想使用Person p = (Person) 18 来创建一个age为18的Person对象,怎么办? 更进一步,我想使用Per ...
- C#中的Explicit和Implicit
今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...
随机推荐
- ECNU 3462 最小 OR 路径 (贪心 + 并查集)
题目链接 EOJ Monthly 2018.1 Problem F 先假设答案的每一位都是$1$,然后从高位开始,选出那些该位置上为$0$的所有边,并查集判断连通性. 如果$s$和$t$可以连通的话 ...
- Hadoop之Vmware通过仅Use Host-Only networking(使用主机网络)主机链接
Use Host-Only networking(使用主机网络)连接方式 [1]现在宿主机也就是本地电脑上设置IP地址 [2]设置虚拟机 Host-Only 方式 验证 宿 ...
- Spark-shell启动脚本解读
#!/usr/bin/env bash # # Licensed to the Apache Software Foundation (ASF) under one or more # contrib ...
- 软路由OpenWrt教程收集(插件开发教程,opkg安装软件教程)
说明:现在几乎所有家庭级别的路由器都是基于OpenWrt进行衍生搭建的. https://openwrt.io/(极路由HiWifi创建的开源站点,极路由系统有这个衍生而来) http://www.o ...
- java内存缓存,节省内存
缓存的对象 这个问题就是我们上面提到的极端情况,在Java中,会对-128到127的Integer对象进行缓存,当创建新的Integer对象时,如果符合这个这个范围,并且已有存在的相同值的对象,则返回 ...
- java随机生成汉字
public static void main(String[] args) { String str = null; int hs, ls; Random random = new Random() ...
- WIN10 64位 JDK的安装
因为电脑系统换掉,重装系统,重新配置了一下环境,安装JDK,现记录一下过程,以便下次查询使用. 官网下载JDK,地址:http://www.oracle.com/technetwork/java/ja ...
- apache的proxy代理总访问后端web的第一个虚拟主机
先查看cat /usr/local/apache2/modules 时候有mod_proxy.so mod_proxy_http.so mod_proxy_connect 如果没有,使用apache ...
- django 用model来简化form
django里面的model和form其实有很多地方有相同之处,django本身也支持用model来简化form 一般情况下,我们的form是这样的 from django import forms ...
- python git log
# -*- coding: utf-8 -*- # created by vince67 Feb.2014 # nuovince@gmail.com import re import os imp ...