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( ...
随机推荐
- SenseTime Ace Coder Challenge 暨 商汤在线编程挑战赛*
题目链接 Problems Problem A Problem B bitset…… Problem C Problem D Problem E Problem F Problem G 考虑最小生 ...
- Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)
题目链接 2016 ACM-ICPC EC-Final Problem G 题意 给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...
- Python与数据库[1] -> 数据库接口/DB-API[2] -> SQL Server 适配器
SQL_Server适配器 / SQL_Server Adapter 1 环境配置 / Environment Configuration 安装SQL_Server的Python适配器包 pip in ...
- SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- HDU 4251 The Famous ICPC Team Again(划分树)
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- NMAP输出结果中CPE的含义
NMAP输出结果中CPE的含义 CPE全称是Common Platform Enumeration,意思是通用平台枚举项.它是NMAP对识别出来的软件.操作系统和硬件的一种命名方式.它的格式如下: ...
- Bean的实例化--静态工厂
1,创建实体类User package com.songyan.demo1; /** * 要创建的对象类 * @author sy * */ public class User { private S ...
- linux 之创建文件命令
1.vi vi 1.txt 会直接创建并打开一个文件1.txt 2.touch touch的作用是更改一个文件或目录的时间.touch 2.txt 如果2.txt不存在,则创建空文件2.txt 3.e ...
- XCode工程内多Targets教程
作者 透明de面具 原帖地址 http://www.cocoachina.com/bbs/read.php?tid-10972-fpage-0-toread--page-1.html 相信很 ...
- 【IntellJ IDEA】idea的Terminal窗口中文乱码 解决方法
在操作git命令在windows上的idea中的terminal窗口时,总是出现中文乱码的问题 本开始以为是git乱码的问题,如果是GIt中文乱码,解决方案:https://www.cnblogs.c ...