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( ...
随机推荐
- 51nod 1101 换零钱 【完全背包变形/无限件可取】
1101 换零钱 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 ...
- 最小生成树【p2121】 拆地毯
题目描述--->p2121 拆地毯 分析 这题为什么是最大生成树. 先来bb两句 题目为拆地毯,让我们剩下k个地毯. 题目想要我们求得最大的美丽度. 且要求我们 保留的地毯构成的图中,任意可互相 ...
- APIO2018练习赛伪题解
传送门:https://pcms.university.innopolis.ru/statements/org/apio/2018/practice/statements.pdf 主要就在于后面三道构 ...
- matlab坐标轴设置
1. axis([xmin xmax ymin ymax]) 设置当前图形的坐标范围,分别为x轴的最小.最大值,y轴的最小最大值 2. V=axis 返回包含当前坐标范围的一个行向量 3. axis ...
- PS Tips: powershell 将文件以utf8格式打开并另存
1. powershell 将文件以utf8格式打开并另存 Get-Content .\rep_position_liquidity.csv -Encoding UTF8 | Set-Content ...
- git push后自动部署
前提,服务器已经装好ssh,本地也已经将ssh 公钥传到服务器对应位置 先用 pbcopy < ~/.ssh/PRIVATE_KEY.pub 将公钥复制到剪贴板:通过 ssh USER@SERV ...
- git只拉取github部分代码的方法
需求:github某个项目所有代码太大,有600+M,甚至更大:只需要拉取部分代码,一是可以降低网络消耗,二是可以降低磁盘占用 分析了下空间占用情况:发现每个项目下的.git/objects/pack ...
- 智能选择器和语义化的CSS
本文由白牙根据Heydon Pickering的<Semantic CSS With Intelligent Selectors>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之 ...
- Java 学习之网络编程案例
网络编程案例 一,概念 1,网络编程不等于网站编程 2,编程只和传输层打交道,即TCP和UDP两个协议 二,案例 1,TCP实现点对点的聊天 Server端:两个输入流:读客户端和控制台,一个输出端: ...
- http接口测试—自动化测试框架设计
转载:https://my.oschina.net/hellotest/blog/499719 一.测试需求描述 对服务后台一系列的http接口功能测试. 输入:根据接口描述构造不同的参数输入值(Js ...