c#关键词implicit和explicit

implicit [ɪmˈplɪsɪt] adj.不言明[含蓄]的; 无疑问的,绝对的; 成为一部份的; 内含的; 

explicit [ɪkˈsplɪsɪt] adj.明确的,清楚的; 直言的; 详述的; 不隐瞒的; 


百度:http://baike.baidu.com/link?url=YzBfLNJG8OePWMApO_IrD2lnLI52IqAYEAdm7XR9C-N0UbtRHE5lwuzuRw6PsAPhCXFoB0JVGYKwCzExnM210K

implicit 关键字用于声明隐式的用户定义类型转换运算符。如果转换过程可以确保不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换。

MSDN参考:

http://msdn.microsoft.com/zh-cn/library/z5z9kes2.aspx implicit

https://msdn.microsoft.com/zh-cn/library/xhbhezf4.aspx explicit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqExpTest
{
    class testimplicit
    {
        public static void kk()
        {
            Digit dig = new Digit(7);
            //This call invokes the implicit "double" operator
            double num = dig;
            //This call invokes the implicit "Digit" operator
            Digit dig2 = 12;
            Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
            Console.ReadLine();
        }
    }
    class Digit
    {
        public double val;
        public Digit(double d)
        {
            val = d;
        }
        
        // User-defined conversion from Digit to double
        public static implicit operator double(Digit d)
        {
            return d.val;
        }
        //  User-defined conversion from double to Digit
        public static implicit operator Digit(double d)
        {
            return new Digit(d);
        }
    }
}
 static void Main(string[] args)
        {
            //ExecuteCode(aa);
            ExecuteCode(testimplicit.kk);
            Console.ReadKey();
        }
输出:

num = 7 dig2 = 12

相关文章 c#中operator关键字: http://blog.csdn.net/joyhen/article/details/47470047

你可能感兴趣的:(Explicit,Implicit)