`
ribishuangba
  • 浏览: 291346 次
文章分类
社区版块
存档分类
最新评论

c#中结构的转化示例

 
阅读更多

c#中结构的转化示例

structconversion.cs
using System;

struct RomanNumeral
... {
public RomanNumeral( int value)
... {
this .value = value;
}

static public implicit operator RomanNumeral( int value)
... {
return new RomanNumeral(value);
}

static public implicit operator RomanNumeral(BinaryNumeralbinary)
... {
return new RomanNumeral(( int )binary);
}

static public explicit operator int (RomanNumeralroman)
... {
return roman.value;
}

static public implicit operator string (RomanNumeralroman)
... {
return ( " Conversionnotyetimplemented " ;
}

private int value;
}


struct BinaryNumeral
... {
public BinaryNumeral( int value)
... {
this .value = value;
}

static public implicit operator BinaryNumeral( int value)
... {
return new BinaryNumeral(value);
}

static public implicit operator string (BinaryNumeralbinary)
... {
return ( " Conversionnotyetimplemented " ;
}

static public explicit operator int (BinaryNumeralbinary)
... {
return (binary.value);
}


private int value;
}


class Test
... {
static public void Main()
... {
RomanNumeralroman;
roman
= 10 ;
BinaryNumeralbinary;
// PerformaconversionfromaRomanNumeraltoa
// BinaryNumeral:
binary = (BinaryNumeral)( int )roman;
// PerformsaconversionfromaBinaryNumeraltoaRomanNumeral.
// Nocastisrequired:
roman = binary;
Console.WriteLine((
int )binary);
Console.WriteLine(binary);
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics