c# - How do you create your own DataType? -


I would like to create my own DataType named positiveInteger

I know what you are thinking?

You are thinking that I should use uint but uint has 0 and I want only positive numbers .

Now you can tell me to create a class named positiveInteger . Yes, I can make a class called positiveInteger , but I do not know how to implement this class that this new datatyuse only accepts positive integer values?

Example can be an example:

  Public Structure PositiveInteger : IEquatable & lt; PositiveInteger & gt; IComparable & lt; PositiveInteger & gt; {Public-positive integer (UIT value) {if (value ; = 0) leave the new argument with the exception exception (); _value = value; } Public UIT Value {Received {Return _value == 0? 1: _value; }} Private Readonly UIT _ Value; Public stable built-in operator-positive integer (UIT value) {returns new positive integer; } Public stable built-in operator UIT (positive etaser value) {return value. Value; } Public Static Positive Integer Operator + (Positive Interactive Value 1, Positive Intake Value 2) {var Result = Value1. Value + value 2. value; If (result & lt; value1.Value || result & lt; value2.Value) {new argument OUTOfRangeException (); // overflow} return result; } Public Static Positive Integer Operator - (Positive Integer Value 1, Positive Integer Val 2) {if (value1.Value  

and a small test:

  zero trials () {var list = new list & lt; PositiveInteger & gt; {5, 1, 3}; List.Sort (); // 1,3,5 var a = new positive integer (1); Var B = New Positive Integer (2); Var C = A + B; // = 3 var d = c - b; // = 1 var E = D - A; // throws ArgumentOutRangeException}  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -