Id#

The id of a task, link, resource, assignment or calendar. An Id holds either a number or a string. Create one with Id.Num or Id.Str, or assign a string, double or int wherever an Id is expected, and the implicit conversion builds it.

Two ids are equal when their Key() values are equal, so 1 and "1" are the same id.

Declaration#

public sealed class Id : IEquatable<Id>

Id is immutable and has no public constructor.

Properties#

Property Type Description
IsNum bool true for a numeric id.
IsStr bool true for a string id. When it's true, the compiler treats StrValue as non-null.
NumValue double? The number of a numeric id; null for a string id.
StrValue string? The text of a string id; null for a numeric id.

Methods#

Key#

public string Key()

Returns the id as text. A string id is returned as is. A number is printed the way JavaScript's String(n) prints it:

Number Key
1 "1"
1.5 "1.5"
0 and -0 "0"
1e21 "1e+21"
1e-7 "1e-7"
double.NaN "NaN"

Equality, GetHashCode() and ToString() all use this key.

Errors: throws InvalidOperationException when the id is an infinite number.

Norm#

public string Norm()

Same as Key().

Equals#

public bool Equals(Id? other)
public override bool Equals(object? obj)

Returns true when both ids have the same key: Id.Num(1).Equals(Id.Str("1")) is true. String ids compare with ordinal, case-sensitive comparison. Two numeric ids compare as numbers, and every NaN equals every other NaN. Equals(null) returns false.

A number beyond double precision is stored rounded, so Id.Num(9007199254740993) equals Id.Str("9007199254740992").

Errors: throws InvalidOperationException when one id is a string and the other is an infinite number.

GetHashCode#

public override int GetHashCode()

Hash of the key, so ids that are equal hash the same and Id works as a Dictionary or HashSet key.

Errors: throws InvalidOperationException for an infinite number.

ToString#

public override string ToString()

Returns the key.

Static methods#

Num#

public static Id Num(double n)

Creates a numeric id.

Str#

public static Id Str(string s)

Creates a string id. The empty string is a valid id.

Errors: throws ArgumentNullException when s is null.

Operators#

public static bool operator ==(Id? a, Id? b)
public static bool operator !=(Id? a, Id? b)

public static implicit operator Id(string s)
public static implicit operator Id(double n)
public static implicit operator Id(int n)

== and != compare by key, like Equals. Two null references are equal; null and an id aren't.

The implicit conversions call Id.Str or Id.Num. Converting a null string throws ArgumentNullException.

Validation#

An id in project data must be a finite number or a string. A NaN or infinite numeric id throws InputException with Path "id" when the data is loaded.

Example#

using Plen;

Id a = 1;          // Id.Num(1)
Id b = "1";        // Id.Str("1")
Id c = 1.5;

Console.WriteLine(a == b);         // True
Console.WriteLine(a.IsNum);        // True
Console.WriteLine(b.StrValue);     // 1
Console.WriteLine(c.Key());        // 1.5

var names = new Dictionary<Id, string> { [a] = "Design" };
Console.WriteLine(names["1"]);     // Design