Class Die
Represents a single die with a fixed number of sides.
public sealed class Die : IDie, IEquatable<Die>
- Inheritance
-
Die
- Implements
- Inherited Members
- Extension Methods
Constructors
Die(int)
Constructs a new instance of Die with the specified number of sides.
public Die(int sideCount = 6)
Parameters
sideCountintThe number of sides on the die
Examples
var d20 = new Die(20);
int result = d20.Roll(); // a value in [1, 20]
Remarks
sideCount of 2 represents a coin toss
Exceptions
- ArgumentOutOfRangeException
sideCount is less than 2
Properties
MaxValue
The maximum value that can be rolled on the die. Equal to SideCount.
public int MaxValue { get; }
Property Value
MinValue
The minimum value that can be rolled on the die. Always 1.
public int MinValue { get; }
Property Value
SideCount
The number of sides on the die. Must be at least 2.
public int SideCount { get; }
Property Value
Remarks
A value of 2 represents a coin toss, 3 represents a three-sided die, etc.
Methods
Equals(object?)
Checks if this instance is equal to another object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with this instance. It can be null or of any type.
Returns
Equals(Die?)
Checks if this instance is equal to another instance of Die.
public bool Equals(Die? other)
Parameters
Returns
GetHashCode()
Generates a hash code for this instance based on its SideCount.
public override int GetHashCode()
Returns
Roll()
Rolls the die and returns the value rolled.
public int Roll()
Returns
Examples
var d6 = new Die(6);
int result = d6.Roll(); // a value in [1, 6]
ToString()
Returns a string representation of the die in the format "dY" where Y is the number of sides.
public override string ToString()
Returns
- string
The die in standard
dYnotation.
Examples
string notation = new Die(20).ToString(); // "d20"