C#explicit和implicit关键字实现类型转换
using System; namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
//implicit 隐式装换
Digit dig1 = new Digit(7.0m);
decimal num = dig1;
Digit dig2 = 12.0m;
Console.WriteLine("num = {0} dig2 = {1}", num, dig2.Val); //explicit 显示转换
AA a1 = new AA
{
A = "A",
B = ,
C = 1.0m
}; BB b1 = new BB
{
A1 = "B",
B1 = ,
C1 = 2.0m
};
AA a2 = (AA)b1;
BB b2 = (BB)a1;
Console.WriteLine($"{a2.A}|{a2.B}|{a2.C}");
Console.WriteLine($"{b2.A1}|{b2.B1}|{b2.C1}");
Console.ReadLine();
}
} class Digit
{
public decimal Val;
public Digit(decimal d) { Val = d; }
public static implicit operator decimal(Digit d)
{
return d.Val;
}
public static implicit operator Digit(decimal d)
{
return new Digit(d);
}
} class AA
{
public string A { get; set; }
public int B { get; set; }
public decimal C { get; set; } public static explicit operator BB(AA c)
{
return new BB
{
A1 = c.A,
B1 = c.B,
C1 = c.C
};
}
} class BB
{
public string A1 { get; set; }
public int B1 { get; set; }
public decimal C1 { get; set; }
public static explicit operator AA(BB c)
{
return new AA
{
A = c.A1,
B = c.B1,
C = c.C1
};
}
} }
C#explicit和implicit关键字实现类型转换的更多相关文章
- C#中转换运算符explicit、implicit、operator、volatile研究
C#中的这个几个关键字:explicit.implicit与operator,估计好多人的用不上,什么情况,这是什么?字面解释:explicit:清楚明白的;易于理解的;(说话)清晰的,明确的;直言的 ...
- C#的关键字Explicit 和 Implicit
一.explicit和implicit explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符:implicit 关键字用于声明隐式的用户自定义的类型转换运算符. 总结来说: ...
- C#中的Explicit和Implicit
今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...
- operator、explicit与implicit
说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...
- C#中的explicit和implicit了解一下吧
今天在研究公司项目框架的时候看到了下面的用法,public static implicit operator JsonData(int data);.貌似很久没用过这种隐式转换的写法了,因此重新温习一 ...
- (二) operator、explicit与implicit 操作符重载
有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成把两个整 ...
- (一) operator、explicit与implicit 操作符重载
原文地址: Click Here 操作符重载必须用public static 应为操作符是用来操作实例的. operator operator ...
- explicit 和 implicit 的用法
explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换 explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A) ...
- 【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 ...
随机推荐
- mysql分页和oracle分页
1,mysql分页 select * from table_test LIMIT ((pageNum - 1) * pageSize),pageSize) 2,oracle分页 select * fr ...
- In Vitro model验证 | Harnessing single-cell genomics to improve the physiological fidelity of organoid-derived cell types
Transcriptional benchmarking of in vitro cells to in vivo with single-cell rna-seq - 简介 Harnessing s ...
- 网页版查看Android源码地址
Android社区 http://www.androidos.net.cn/sourcecode
- GEOS/GDAL 交叉编译ARM64-linux版本
目录 安装编译环境 编译PROJ.4 编译GEOS 编译GDAL 编译后程序运行注意事项 因为试用华为云ARM64服务器(CentOS 7 操作系统)的时候,在云服务器上编译GDAL很长时间也没有编译 ...
- kotlin基础 尾递归
尾调用的重要性在于它可以不在调用栈上面添加一个新的堆栈帧——而是更新它,如同迭代一般. 尾递归因而具有两个特征: 调用自身函数(Self-called): 计算仅占用常量栈空间(Stack Space ...
- odoo开发笔记 -- 还原数据库后,异常:ir_attachment: IOError: [Errno 2] No such file or directory: u'/var/...'
场景描述: 恢复Odoo数据后,抛出错误导致无法进入页面 -- ::, INFO aeo odoo.addons.base.ir.ir_attachment: _read_file reading / ...
- Java13新特性 -- switch表达式
引入了yield语句,用于返回值: 和return的区别在于:return会直接跳出当前循环或者方法,而yield只会跳出当前switch块. @Test public void testSwitch ...
- LeetCode 104. Maximum Depth of Binary Tree(二叉树深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- vue install 组件
import share from './index.vue' export default { install: (Vue) => { Vue.prototype.$share = (opti ...
- 爬虫相关-scrapy框架介绍
性能相关-进程.线程.协程 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. 串行执行 import requests def fetc ...