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( ...
随机推荐
- Lct浅谈
Lct浅谈 1.对lct的认识 首先要知道$lct$是什么.$lct$的全称为$link-cut-tree$.通过全称可以看出,这个数据结构是维护树上的问题,并且是可以支持连边断边操作.$lct$ ...
- Bug预防体系
Web常见产品问题及预防 测试人员在每次版本迭代中,会对项目的整体质量有一个把控,对于项目常见的问题,开发经常犯的错误都会有所了解,为了避免或者减少这样的错误或不规范的事情在发生,测试人员可以整理构建 ...
- 离别在须臾——AFO
始于 2016-12-25 终于 2017-11-30 一开始就陷入了颓废的坑,然后,直至 2017 年暑假,颓废的气息一发不可收拾, 从 Q_fight 到 aoqi_baitian ,从 大主宰 ...
- POJ 2104 K-th Number (划分树)
K-th Number Time Limit: 20000MS Memory ...
- oracle Update a table with data from another table
UPDATE table1 t1 SET (name, desc) = (SELECT t2.name, t2.desc FROM table2 t2 WHERE t1.id = t2.id) WHE ...
- js timestamp 转换 date 和 将秒数整理为时分秒格式
// 获得的后台json 时间格式转化 例如:1520305366000 转化为XXXX-XX-XX类似这种 function timeStamp2String(time){ var datetim ...
- Runtime对象
Runtime简单概念: Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. * 这也是jvm实现跨平台的一个重要原因. * 可以通过 ge ...
- nginx静态文件访问404
在http模块下加入下面的代码 server { listen 80; server_name 192.168.1.249; #本机ip #access_log logs/host.access.lo ...
- 解决413 Request Entity Too Large
修改nginx配置 这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好. 1.打开nginx主配置文件nginx.conf,一般在/u ...
- Kubernentes中的身份验证
Kubernentes中的身份验证 kubernetes 系统的各组件需要使用 TLS 证书对通信进行加密,本文档使用 CloudFlare 的 PKI 工具集 cfssl 来生成 Certifica ...