1、它们解决什么问题?

  考虑下面的需求,Person类有个字段age。我想使用Person p = (Person) 18 来创建一个age为18的Person对象,怎么办?

  更进一步,我想使用Person p = 18 来创建一个age为18的Person对象,怎么办?

2、使用explicit(显式)和implicit(隐式)

    class Person
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
} public static explicit operator Person(int age)
{
return new Person() { age = age, };
} //public static implicit operator Person(int age)
//{
// return new Person() { age = age, };
//}
} class Program
{
static void Main(string[] args)
{
Person p = (Person); // 调用explicit
//Person p = 18; // 调用implicit
}
}

注意:二者不同同时提供,否则编译错误。这种语法其实是借鉴了C++的方式,并进行了扩展。一般情况下,不要使用这种类型转换,因为不直观。

C# explicit与implicit的更多相关文章

  1. 【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 ...

  2. operator、explicit与implicit

    说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...

  3. explicit 和 implicit 的用法

    explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换 explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A) ...

  4. C#中转换运算符explicit、implicit、operator、volatile研究

    C#中的这个几个关键字:explicit.implicit与operator,估计好多人的用不上,什么情况,这是什么?字面解释:explicit:清楚明白的;易于理解的;(说话)清晰的,明确的;直言的 ...

  5. C#的关键字Explicit 和 Implicit

    一.explicit和implicit explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符:implicit 关键字用于声明隐式的用户自定义的类型转换运算符. 总结来说: ...

  6. 可空类型(Nullable<T>)及其引出的关于explicit、implicit的使用

    问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int&g ...

  7. C#中的Explicit和Implicit

    今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...

  8. C#中的explicit和implicit了解一下吧

    今天在研究公司项目框架的时候看到了下面的用法,public static implicit operator JsonData(int data);.貌似很久没用过这种隐式转换的写法了,因此重新温习一 ...

  9. C#explicit和implicit关键字实现类型转换

    using System; namespace ConsoleTest { class Program { static void Main(string[] args) { //implicit 隐 ...

随机推荐

  1. eclipse easyexplorer openexplorer

    电脑重装了.. eclipse换成了4.4.2,发现原来的easyexplorer不生效了(原来是4.2),搜了下,换成了open explorer即可. 方式一样,都是丢到eclipse/plugi ...

  2. Cookies简介和使用

    Cookie public class javax.servlet.http.Cookie  1作用(1)Cookie能使站点跟踪特定访问者的访问次数.最后访问时间和访问者进入站点的路径(2)Cook ...

  3. wpf 窗口程序下将datagrid导出为excel

    今天用了几个小时也没有找到将datagrid导出为excel的方法,搜索msdn发现,老外也木有解决这个问题,因此把代码贴出来,和大家分享一下,提高工作效率.简要说一哈,本程序使用反射,因此代码量看起 ...

  4. WCF概念

    WCF 概念 WCF是.NET Framework 上灵活通讯技术.在.NET 3.0推出之前,一个企业解决方案需要几种通讯方式.对于独立于平台的通讯,使用ASP.NET Web服务.对于比较高级的 ...

  5. Bonobo Git Server (Simple git server for Windows.) 测试备忘

    Bonobo Git Server是一款Windows上的Git Server,它使用IIS即可,走的是Http协议,只要简单的安装就能使用,但是因为我的项目大小有1.35GB在 push 的时候一直 ...

  6. GTD3年来读的52本书

    2012年   1.一生的计划 平衡:人生要在精神.理财.教育和娱乐4个方面进行平衡.   2.重来REWORK 小型软件公司的创业与软件项目的管理 不要管全年计划,只要找出下一项最重要的任务,然后起 ...

  7. ListView属性整理

    stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false android:stackFromBottom="true"   第 ...

  8. Android项目实战(九):CustomShapeImageView 自定义形状的ImageView

    一个两年前出来的第三方类库,具有不限于圆形ImageView的多种形状ImageView,项目开发必备 github下载地址:https://github.com/MostafaGazar/Custo ...

  9. ReactiveCocoa入门教程:第二部分

    翻译自:http://www.raywenderlich.com/62796/reactivecocoa-tutorial-pt2 原文链接: ReactiveCocoa 是一个框架,它允许你在你的i ...

  10. 【读书笔记】iOS-GCD-系统提供的dispatch方法

    系统提供的dispatch方法如下: //系统提供的dispatch方法 //后台执行: dispatch_async(dispatch_get_global_queue(0, 0), ^{ // s ...