【转】C#中的implicit 和 explicit
The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say that you have the following class:
public class Role
{
public string Name { get; set; }
}
If you want to create a new Role and assign a Name to it, you will typically do it like this:
Role role = new Role();
role.Name = "RoleName";
Since it has only one property, it would perhaps be convenient if we could instead do it like this:
Role role = "RoleName";
This means that we want to implicitly convert a string to a Role (since there is no specific cast involved in the code). To achieve this, we add an implicit conversion operator:
public static implicit operator Role(string roleName)
{
return new Role() { Name = roleName };
}
Another option is to implement an explicit conversion operator:
public static explicit operator Role(string roleName)
{
return new Role() { Name = roleName };
}
In this case, we cannot implicitly convert a string to a Role, but we need to cast it in our code:
Role r = (Role)"RoleName";
【转】C#中的implicit 和 explicit的更多相关文章
- C++雾中风景5:Explicit's better than implicit.聊聊Explicit.
关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议.那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊.而很多时候,Implicit的很多 ...
- Scala中的Implicit详解
Scala中的implicit关键字对于我们初学者像是一个谜一样的存在,一边惊讶于代码的简洁, 一边像在迷宫里打转一样地去找隐式的代码,因此我们团队结合目前的开发工作,将implicit作为一个专题进 ...
- C# 自己定义 implicit和explicit转换
explicit 和 implicit 属于转换运算符,如用这两者能够让我们自己定义的类型支持相互交换explicti 表示显式转换.如从 A -> B 必须进行强制类型转换(B = (B)A) ...
- implicit和 explicit关键字
implicit 关键字用于声明隐式的用户定义类型转换运算符. 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换. class Digit { publi ...
- C#自定义转换(implicit 或 explicit)
C#的类型转换分为显式转换和隐式转换,显式转换需要自己声明转换类型,而隐式转换由编译器自动完成,无需我们声明,如: //long需要显式转换成int long l = 1L; int i = (int ...
- Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...
- implicit和explicit的基本使用
class MyAge { public int Age { get; set; } public static implicit operator MyAge(int age) { return n ...
- C#之自己定义的implicit和explicit转换
在类型转换时常会遇到隐式转换和显式转换.那我们自己定义的类型要怎样去定义隐式转换和显式转换?我们来看一段代码 public class Rational { private Int32 _inner_ ...
- C++中的关键字用法--- explicit
1. C++中的explicit C++提供了关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生.声明为explicit的构造函数不能在隐式转换中使用. C++中, 一个 ...
随机推荐
- tsung基准测试方法、理解tsung.xml配置文件、tsung统计报告简介
网上搜集的资料,资料来源于:http://blog.sina.com.cn/ishouke 1.tsung基准测试方法 https://pan.baidu.com/s/1Ne3FYo8XyelnJy8 ...
- Python+selenium之下载文件
一.Firefox文件下载 Web容许我们设置默认的文件下载路劲,文件会自动下载并且存放在指定的目录下. from selenium import webdriver import os fp = w ...
- APP自动化测试
CTS工具,主要是基于Androidinstrumentation和JUnit测试原理推单元测试用例: Monkey用来对UI进行压力测试,伪随机的模拟用户的按键输入,触摸屏输入,手势输入等: ASE ...
- 个人作业-Alpha项目测试
姓名 蒋东航 学号 201731062328 这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业要求链接 团队名称 机你太美(团队博客链接) 这个作业的目标 了解其他团队项目,学习其他团队优秀方 ...
- RSA AES 前端JS与后台JAVA的加密解密的是实现
AES CryptoJS 前提是编码方式,key,vi中设置一样,就可以进行跨语言加密解密 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- 【转】JavaScript 节点操作 以及DOMDocument属性和方法
最近发现DOMDocument对象很重要,还有XMLHTTP也很重要 注意大小写一定不能弄错. 属性: 1Attributes 存储节点的属性列表(只读) 2childNodes 存储节点的子节点列表 ...
- python 产生随机数
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- JQuery EasyUI学习记录(三)
1.jQuery EasyUI messager使用方式 1.1 alert方法 $(function(){ //1.alert方法---提示框 $.messager.alert("标题&q ...
- 有关SQL的一道面试题
这是一个学生分数表 StudentName StudySubject SubjectScore Peter ...
- C#基于联通短信Sgip协议构建短信网关程序
此软件基于中国联通Sgip协议程序接口,适合在中国联通申请了短信发送端口的公司使用.短信群发已经成为现在软件系统.网络营销等必不可少的应用工具.可应用在短信验证.信息群发.游戏虚拟商品购买.事件提醒. ...