Underlying types
editAn ID wraps one value of a supported type. Any other type makes the analyzer report MFSTID0001, and no code is generated.
Supported types
| Underlying type | Factory method | JSON value (System.Text.Json) |
|---|---|---|
bool | FromBoolean | true or false |
byte | FromByte | number |
sbyte | FromSByte | number |
short | FromInt16 | number |
ushort | FromUInt16 | number |
int | FromInt32 | number |
uint | FromUInt32 | number |
long | FromInt64 | number |
ulong | FromUInt64 | number |
Int128 | FromInt128 | number, with all its digits |
UInt128 | FromUInt128 | number, with all its digits |
System.Numerics.BigInteger | FromBigInteger | number, with all its digits |
Half | FromHalf | number |
float | FromSingle | number |
double | FromDouble | number |
decimal | FromDecimal | number |
Guid | FromGuid | string |
string | FromString | string |
DateTime | FromDateTime | string (ISO 8601) |
DateTimeOffset | FromDateTimeOffset | string (ISO 8601) |
MongoDB.Bson.ObjectId | FromObjectId | string |
MongoDB.Bson.ObjectId needs a reference to the MongoDB.Bson package. Int128, UInt128 and Half need a target framework that has them.
Pick a type
intorlongfor the keys of a database table that generates them.Guidfor IDs created by the application: the generatedNew()method creates one. See Create Guid values.stringfor codes and external references (SKUs, country codes, user names from an identity provider). Choose how they are compared with theStringComparisonoption.
How values are formatted and parsed
ValueAsString, ToString() and the converters that write strings format the value with the invariant culture:
- Numbers use the invariant culture, without a format:
42,1.5. Guiduses its default format:0f8fad5b-d9cb-469f-a165-70867728950e.boolisTrueorFalse.DateTimeuses the round-trip format ("o").DateTimeOffsetis converted to UTC first, so the offset is not in ValueAsString.
Parse and TryParse read the same representations with the invariant culture:
- Numbers accept any number style (
NumberStyles.Any): signs, thousands separators, decimal points, exponents and surrounding white space. DateTimeandDateTimeOffsetare adjusted to UTC (DateTimeStyles.AdjustToUniversal).boolandGuidusebool.TryParseandGuid.TryParse.- A
stringID accepts any string, but notnull.
ObjectId IDs have no Parse(ReadOnlySpan<char>) overload and do not implement ISpanParsable<T>, because ObjectId has no such method.