如何在.NET中创建自己的原始数据类型?

时间:2008-12-25 18:55:12

标签: .net vb.net

如何创建自己的原语?例如,范围为1-10的整数。

编辑:这来自Rosetta Code的任务。

  

定义基元数据类型:演示如何定义行为类似于整数但有效值最低为1且有效值最高为10的类型。

我在这里添加了它,因为我认为它可能对其他人有用。

3 个答案:

答案 0 :(得分:7)

好的,我们来看看。首先,CLR中有一些数据类型。那些不能被修改或添加新的,因为它们是标准的一部分。您可以找到列表herehere。那是C#,但是这个列表也应该存在于某个地方的VB.net中,它应该看起来相同,因为底层的CLR是相同的。此外,列表不完整,因为缺少浮点数和字符,但你明白了。

但是,有一些结构封装了这些数据类型并添加了一些额外的功能。例如,System.Int32只是一个简单的标准结构,不涉及任何魔法。请随意在Reflector中查看它,它位于mscorlib:

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>

所以你想要自己的“1到10”整数?然后,我建议您查看最近的合适类型,即Int16Byte。如果你看一下它们,你可以看到它们看起来有点类似,但是它们基于一种内置数据类型。

只是复制/粘贴和修改某些内置结构(即System.Byte)并不完全有效,因为某些成员是内部的(即NumberFormatInfo.ValidateParseStyleInteger),但Reflector可以帮助这里。

答案 1 :(得分:6)

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

答案 2 :(得分:2)

通过使用Delphi.Net或Delphi Prism(两种.Net语言),您可以定义如下变量:

MyAge : 0..120;
NumOfFingersAfterNewYear : 0..10;

一个很好的优点是你可以用它进行编译时范围检查。