In This Article

Unit Struct

Represents a length measurement.

[TypeConverter(typeof(UnitConverter))]
public readonly struct Unit : IEquatable<Unit>
Implements:
IEquatable<Unit>

Constructors

Unit(double)

Initializes an instance of the structure.

public Unit(double value)
Parameter Type Description
value double

The value.

Unit(double, UnitType)

Initializes an instance of the structure.

public Unit(double value, UnitType type)
Parameter Type Description
value double

The value.

type UnitType

The type.

Unit(string)

Initializes an instance of the structure.

public Unit(string value)
Parameter Type Description
value string

The value.

Unit(string, CultureInfo)

Initializes an instance of the structure.

public Unit(string value, CultureInfo culture)
Parameter Type Description
value string

The value.

culture CultureInfo

The culture.

Properties

IsEmpty

Indicates indicating whether this instance is empty.

public bool IsEmpty { get; }

Property Value

bool:

true if this instance is empty; otherwise, false.

Type

The type of units used by Value.

public UnitType Type { get; }

Property Value

UnitType

Value

The value.

public double Value { get; }

Property Value

double

Methods

Centimeter(double)

Creates a new Unit instance with the specified centimeters.

public static Unit Centimeter(double value)
Parameter Type Description
value double

The centimeter value.

Returns

Unit

Equals(Unit)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(Unit other)
Parameter Type Description
other Unit

An object to compare with this object.

Returns

bool:

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameter Type Description
obj object

The object to compare with the current instance.

Returns

bool:

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int:

A 32-bit signed integer that is the hash code for this instance.

GetPixels(double)

Gets the number of pixels represented by this Unit using the specified width when the Type is set to Percentage.

public double GetPixels(double width)
Parameter Type Description
width double

The width.

Returns

double

Inch(double)

Creates a new Unit instance with the specified inches.

public static Unit Inch(double value)
Parameter Type Description
value double

The inch value.

Returns

Unit

Parse(string)

Parses the specified string.

public static Unit Parse(string value)
Parameter Type Description
value string

The string to parse.

Returns

Unit

Parse(string, CultureInfo)

Parses the specified string using the specified CultureInfo.

public static Unit Parse(string value, CultureInfo culture)
Parameter Type Description
value string

The string to parse.

culture CultureInfo

The culture.

Returns

Unit

Percentage(double)

Creates a new Unit instance with the specified percentage.

public static Unit Percentage(double value)
Parameter Type Description
value double

The percentage value.

Returns

Unit

Pixel(double)

Creates a new Unit instance with the specified pixels.

public static Unit Pixel(double value)
Parameter Type Description
value double

The pixel value.

Returns

Unit

Point(double)

Creates a new Unit instance with the specified points.

public static Unit Point(double value)
Parameter Type Description
value double

The point value.

Returns

Unit

ToString()

Returns the string representation of this object.

public override string ToString()

Returns

string:

The string representation of this object.

ToString(IFormatProvider)

Returns a string representation of this instance using the specified IFormatProvider to convert the Value.

public string ToString(IFormatProvider formatProvider)
Parameter Type Description
formatProvider IFormatProvider

The format provider.

Returns

string

ValidateIsGreaterThan(object, double)

Validates that the object is a Unit greater than the specified number.

public static bool ValidateIsGreaterThan(object value, double min)
Parameter Type Description
value object

The object to validate.

min double

The number that the value must be greater than.

Returns

bool:

true if the validation is successful; otherwise, false.

ValidateIsGreaterThanOrEqual(object, double)

Validates that the object is a Unit greater than or equal to the specified number.

public static bool ValidateIsGreaterThanOrEqual(object value, double min)
Parameter Type Description
value object

The object to validate.

min double

The number that the value must be greater than or equal to.

Returns

bool:

true if the validation is successful; otherwise, false.

ValidateIsPositive(object)

Validates that the object is a Unit greater than or equal to 0.

public static bool ValidateIsPositive(object value)
Parameter Type Description
value object

The object to validate.

Returns

bool:

true if the validation is successful; otherwise, false.

Operators

operator ==(Unit, Unit)

Compares two Unit objects to determine whether they are equal.

public static bool operator ==(Unit left, Unit right)
Parameter Type Description
left Unit

The left.

right Unit

The right.

Returns

bool:

true if both Unit objects are equal; otherwise, false.

implicit operator Unit(double)

Performs an implicit conversion from double to Unit.

public static implicit operator Unit(double value)
Parameter Type Description
value double

The value.

Returns

Unit

operator !=(Unit, Unit)

Compares two Unit objects to determine whether they are not equal.

public static bool operator !=(Unit left, Unit right)
Parameter Type Description
left Unit

The left.

right Unit

The right.

Returns

bool:

true if both Unit objects are not equal; otherwise, false.

Fields

Empty

Represents an empty Unit.

public static readonly Unit Empty

Inherited Members