如何创建自己的原语?例如,范围为1-10的整数.
编辑:这来自Rosetta Code的任务.
定义基元数据类型:演示如何定义行为类似于整数但最低有效值为1且最高有效值为10的类型.
我在这里添加它是因为我认为它可能对其他人有用.
好吧,让我们来看看.首先,CLR中有一些数据类型.那些不能被修改或添加新的,因为它们是标准的一部分.您可以在此处或此处找到列表.那是C#,但是这个列表也应该存在于某个地方的VB.net中,它应该看起来相同,因为底层的CLR是相同的.此外,列表不完整,因为浮动和字符丢失,但你明白了.
但是,有一些结构封装了这些数据类型并添加了一些额外的功能.例如,System.Int32只是一个简单的标准结构,不涉及任何魔法.随意在Reflector中查看它,它在mscorlib中:
[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)] public struct Int32 : IComparable, IFormattable, IConvertible, IComparable, IEquatable
所以你想要自己的"1到10"整数?然后我建议查看最近的合适类型,即Int16
或者Byte
.如果你看一下它们,你会发现它们看起来有些相似,但是它们基于一种内置数据类型.
只是复制/粘贴和修改一些内置结构(即System.Byte
)并不完全有效,因为一些成员是内部的(即NumberFormatInfo.ValidateParseStyleInteger
),但Reflector可以在这里提供帮助.
Structure LimitedInt Implements IComparable(Of LimitedInt) Implements IEquatable(Of LimitedInt) Private m_Value As Integer 'treat the default, 0 as being really 1' Public ReadOnly Property Value() As Integer Get Return If(m_Value = 0, 1, m_Value) End Get End Property Public Sub New(ByVal value As Integer) If value < 1 Or value > 10 Then Throw New ArgumentOutOfRangeException("value") End If m_Value = value End Sub Public Function CompareTo(ByVal other As LimitedInt) As Integer _ Implements System.IComparable(Of LimitedInt).CompareTo Return Me.Value - other.Value End Function Public Overloads Function Equals(ByVal other As LimitedInt) As Boolean _ Implements System.IEquatable(Of LimitedInt).Equals Return Me.Value = other.Value End Function Public Overrides Function GetHashCode() As Integer Return Value.GetHashCode End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If TypeOf obj Is LimitedInt Then Return CType(obj, LimitedInt) = Me End Function Public Shared Operator =(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Equals(right) End Operator Public Shared Operator <>(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return Not (left = right) End Operator Public Shared Operator +(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value + right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator -(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value - right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator *(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value * right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator /(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Double Return left.Value / right.Value End Operator Public Shared Operator \(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value \ right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator Mod(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value Mod right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator And(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value And right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator Or(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value Or right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator Xor(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value Xor right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator ^(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Double Return left.Value ^ right.Value End Operator Public Shared Operator <(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value < right.Value End Operator Public Shared Operator >(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value > right.Value End Operator Public Shared Operator <=(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value <= right.Value End Operator Public Shared Operator >=(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value >= right.Value End Operator Public Shared Widening Operator CType(ByVal left As LimitedInt) As Integer Return left.Value End Operator Public Shared Narrowing Operator CType(ByVal left As Integer) As LimitedInt Return New LimitedInt(left) End Operator End Structure