扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
000: // UserConversions\conversion.cs 001: using System; 002: 003: struct RomanNumeral 004: { 005: public RomanNumeral(int value) 006: { 007: this.value = value; 008: } 009: static public implicit operator RomanNumeral(int value) 010: { 011: return new RomanNumeral(value); 012: } 013: static public explicit operator int(RomanNumeral roman) 014: { 015: return roman.value; 016: } 017: static public implicit operator string(RomanNumeral roman) 018: { 019: return("Conversion not yet implemented"); 020: } 021: private int value; 022: } 023: 024: class Test 025: { 026: static public void Main() 027: { 028: RomanNumeral numeral; 029: 030: numeral = 10; 031: 032: // 显式地从numeral到int的转换033: Console.WriteLine((int)numeral); 034: 035: // 隐示地转换到string036: Console.WriteLine(numeral); 037: 038: // 显示地转换到int,然后显示地转换到short040: short s = (short)numeral; 041: 042: Console.WriteLine(s); 043: 044: } 045: } |
000: // UserConversions\structconversion.cs 001: using System; 002: 003: struct RomanNumeral 004: { 005: public RomanNumeral(int value) { this.value = value; } 006: static public implicit operator RomanNumeral(int value) 007: {return new RomanNumeral(value);} 008: static public implicit operator 009: RomanNumeral(BinaryNumeral binary) 010: {return new RomanNumeral((int)binary);} 011: static public explicit operator int(RomanNumeral roman) 012: {return roman.value;} 013: static public implicit operator string(RomanNumeral roman) 014: {return("Conversion not yet implemented");} 015: private int value; 016: } 017: 018: struct BinaryNumeral 019: { 020: public BinaryNumeral(int value) {this.value = value;} 021: 022: static public implicit operator BinaryNumeral(int value) 023: {return new BinaryNumeral(value);} 024: static public implicit operator string(BinaryNumeral binary) 025: {return("Conversion not yet implemented");} 026: static public explicit operator int(BinaryNumeral binary) 027: {return(binary.value);} 028: 029: private int value; 030: } 031: 032: class Test 033: { 034: static public void Main() 035: { 036: RomanNumeral roman; 037: roman = 10; 038: BinaryNumeral binary; 039: binary = (BinaryNumeral)(int)roman; 040: roman = binary; 041: Console.WriteLine((int)binary); 042: Console.WriteLine(binary); 043: } 044: } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。