- 9
- 0
- 约1.78万字
- 约 54页
- 2017-02-04 发布于江苏
- 举报
* * * * * * * * * * * * * * * * * * * * * * * * 6. User-defined Casts Multiple casting (cont.) public static implicit operator Currency (uint value) { return new Currency(value, 0); } public static implicit operator uint (Currency value) { return value.Dollars; } Currency balance = new Currency(50, 35); ulong bal = (ulong) balance; Currency→uint→ulong or Currency→float→ulong 6. User-defined Casts Multiple casting (cont.) Currency balance = new Currency(50,35); Console.WriteLine(balance); Console.WriteLine(“balance is “ + balance); Console.WriteLine(“balance is (using ToString()) “ + balance.ToString()); uint balance3 = (uint) balance; Console.WriteLine(“Converting to uint gives “ + balance3); 50 balance is $50.35 balance is (using ToString()) $50.35 Converting to uint gives 50 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3. Boxing and Unboxing Warnings The type the variable uses to box will remain the same when unboxing the same variable. int x = 5; // declaring System.Int32 double y = 0; // declaring System.Double object o = x; // Implicit Boxing y = (double)o; // Explicit Unboxing to double y = (double)(int)o; // Explicit Unboxing and then casting to double 3. Boxing and Unboxing Warnings When unboxing, you have to be careful that the receiving value variable has enough room to store all the bytes in the value being unboxed. C#’s ints are only 32 bits long, so unboxing a long value (64 bits) into an int as shown here will result in an InvalidCastException. long myLongNumber = 333333423; object myObject = (object)myLongNumber; int myIntNumber = (int)myObject; 3. Boxing and Unboxing Advantage: Overload function Private void DisposeFunc(object o) { switch(o.getType().ToString()) { case “A”: ? //handle A; case “B”: ? //handle B; case “C”: ? //handle C; … } } 4. Comparing Objects for Equality Comparing reference types for equality ReferenceEquals() Virtual Equals() Static Equals() Comparison operator (==) Comparing value type
您可能关注的文档
最近下载
- 上海大学2022-2023学年第1学期《高等数学(上)》期末考试试卷(A卷)附参考答案.pdf
- 中国教育行业人才流动与薪酬水平_2025年12月.docx
- 2024全国初中数学联赛初二卷 .pdf VIP
- 全国初中数学联合竞赛真题及答案(初二组)2015-年.pdf VIP
- AIAG-VDA-SPC手册-Yellow-Volume2026年2月第一版 中文.pdf VIP
- 美甲美睫投资回报2026年培训课件.pptx VIP
- 中国王氏家谱字辈大全.doc VIP
- 《Michael_Porter_Creating_Shared_Value》.pdf VIP
- 直播带货虚假宣传法律规制研究.pdf
- 新大洲本田MS01说明书用户手册.pdf
原创力文档

文档评论(0)