借用implicit创建我们自己的布尔数据类型-create MyBool data type by implicit
2011-01-23 22:53
656 查看
此idea是由我处理一个帖子的时候突然蹦出来的,感觉可能有的朋友需要,就将我的回复帖在我的blog,由于时间问题,我没有写中文版的,而是直接用的英文,希望大家见谅,不过代码会告诉大家我的思路,其实也没多少可以解释的,我认为这只是一个小技巧罢了。
I use the Implicit Conversion Operator to help us do the similar things as we inherit from the Boolean type, but MyBool type is not a Boolean type.
And then we can override some methods like Equal method, ToString method and so on.
大家有兴趣的话,可以点击下面的链接进入论坛原帖:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a7ca771d-394d-4a5a-b1f7-4c6729cf50cd/
PS:我的同事开发了一个MSDN论坛的小工具,有兴趣的朋友可以试试,此工具已开始在国内推行:
I use the Implicit Conversion Operator to help us do the similar things as we inherit from the Boolean type, but MyBool type is not a Boolean type.
And then we can override some methods like Equal method, ToString method and so on.
class TestProgram { static void Main(string[] args) { MyBool mb = true; mb.PrintMessage("hello MyBool DataType!"); Console.WriteLine(mb.ToString()); Console.ReadLine(); } } public class MyBool { private bool myBool; public MyBool() { } public MyBool(bool b) { myBool = b; } // implicit bool to MyBool conversion operator public static implicit operator MyBool(bool b) { return new MyBool(b); // implicit conversion } // implicit MyBool to bool conversion operator public static implicit operator bool(MyBool mb) { return mb.myBool; // implicit conversion } public void PrintMessage(string msg) { Console.WriteLine(msg); } public string ToString() { return myBool.ToString(); } }
大家有兴趣的话,可以点击下面的链接进入论坛原帖:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a7ca771d-394d-4a5a-b1f7-4c6729cf50cd/
PS:我的同事开发了一个MSDN论坛的小工具,有兴趣的朋友可以试试,此工具已开始在国内推行:
相关文章推荐
- 创建数据库的时候报错,错误提示为“datatype notmach”,说明数据类型不匹配
- Excel互操作中的数据类型(DataType)
- 数据类型演示DataTypeDemo
- JavaSE第二讲:原生数据类型 Primitive Data Type
- JavaScript-Data-Type(数据类型)
- CORBA Programming with TAO - 3.IDL Data Type(数据类型与Mapping)
- firebird的数据类型(datatype)
- 浅谈C语言编程中的布尔bool数据类型
- JavaSE第二讲:原生数据类型 Primitive Data Type
- Oracle 12C 新特性之扩展数据类型(extended data type)
- java 第二讲 原生数据类型 Primitive Data Type
- (二)数据类型(data type)
- 大三学长带我学习JAVA.作业2. 第二讲(原生数据类型 Primitive Data Type)。第三讲(原生数据类型使用陷阱 Pitfall of Primitive Data Type)。大三学长带我学习JAVA.作业2.
- SAP HANA创建类型(SAP HANA CREATE TYPE)
- 数据类型(Data Type)
- 《C++入门经典》读书笔记 第11章 创建自己的数据类型
- 圣思园张龙-Java SE 第二讲(原生数据类型 Primitive Data Type)
- JavaSE第二讲:原生数据类型 Primitive Data Type
- Oracle 12C 新特性之扩展数据类型(extended data type)
- JavaSE第三讲:原生数据类型使用陷阱 Ptifall of Primitive Data Type