C# explicit与implicit

1、它们解决什么问题?

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

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

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

 1 class Person
 2     {
 3         private int age;
 4         public int Age
 5         {
 6             get { return age; }
 7             set { age = value; }
 8         }
 9
10         public static explicit operator Person(int age)
11         {
12             return new Person() { age = age, };
13         }
14
15         //public static implicit operator Person(int age)
16         //{
17         //    return new Person() { age = age, };
18         //}
19     }
20
21     class Program
22     {
23         static void Main(string[] args)
24         {
25             Person p = (Person)18; // 调用explicit
26             //Person p = 18; // 调用implicit
27         }
28     }

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

时间: 2024-08-04 17:54:48

C# explicit与implicit的相关文章

operator、explicit与implicit

说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样就是把float类型的f隐式转换成double类型了!但其实系统帮我们做了类似如下的工作: float f = 1.0: double d=(double)f: 对于表示数值的基本数据类型来说,数值范围小的数据类型转换成数值范围大的数据类型可以进行隐式转换,而反过来则必须进行显示转换 2.显式类型转

C#关键字explicit、implicit、operator 、unsafe 、fixed

今天遇到一些一般不常用,但说不定什么情况下就能用到的C#关键字. 转换关键字 explicit 定义强制转换 // Must be defined inside a class called Fahrenheit: public static explicit operator Celsius(Fahrenheit fahr) { return new Celsius((5.0f / 9.0f) * (fahr.degrees - 32)); } implicit 定义隐性转换 // User-

类型转换关键字explicit与implicit的用法

这篇是个语法文,虽然这两个关键字比较陌生,但让自己有个印象不是坏事 explicit 明确转换与 implicit 隐含转换都是c#内的关键字,目的是让一个类型 转换到另外一个类型,最常见的例子就是由double转换到int时要使用明确转换, 而int转换到double的时候是使用隐含转换. double d = default(double); int i = default(int); //double 转换为 int 时必须明确定义类型 //否则编译器会丢出错误 i = (int)d; /

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

问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int>,就像int之于Int32: Nullable<T>是非常特殊结构类型,它可赋值为null(所以此前我还以为是引用类型),其本质是等同于new: 通过调试可发现上述两个值均为null,但是事实上我们却可以调用他们的一些属性方法比如“HasValue”,由此可见“=null“只是障眼法罢了: 此

C# 参考之转换关键字:operator、explicit与implicit

operator operator 关键字用于在类或结构声明中声明运算符.运算符声明可以采用下列四种形式之一: public static result-type operator unary-operator ( op-type operand ) public static result-type operator binary-operator ( op-type operand, op-type2 operand2 ) public static implicit operator co

C#中的Explicit和Implicit

今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault(e => e.LedgerEntryID == dto.LedgerEntryID); dto.Payment = entity?.Payment; 其中dto.Payment是一个PaymentDTO类的实例,entity?.Payment是一个Payment类的实例,PaymentDTO类和Pa

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

今天在研究公司项目框架的时候看到了下面的用法,public static implicit operator JsonData(int data);.貌似很久没用过这种隐士转换的写法了,因此重新温习一下C#中转换相关的知识. 作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/10898582.html implicit implicit 关键字用于声明隐式的用户自定义的类型转换运算符. 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型

Hibernate: Implicit &amp; Explicit Polymorphism

As I was going through the various inheritance strategies in Hibernate, I came across the ‘class’ element’s attribute, polymorphism=”implicit|explicit”. From the Hibernate’s reference manual, this is what I found as definitions for implicit and expli

C# 自定义 implicit和explicit转换

explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A)implicit 表示隐式转换,如从 B -> A 只需直接赋值(A = B) 隐式转换可以让我们的代码看上去更漂亮.更简洁易懂,所以最好多使用 implicit 运算符.不过!如果对象本身在转换时会损失一些信息(如精度),那么我们只能使用 explicit 运算符,以便在编译期就能警告客户调用 name