MacAddress¶
The MacAddress type represents a 48-bit MAC Address 1 as per the IEEE EUI standard 3. It serves the purpose of a Networking Adjacent worker class, and as a handy way to represent, store, format, and compare MAC addresses.
The MacAddress class implements IEquatable<MacAddress>, IComparable<MacAddress>, IComparable, IFormattable, and ISerializable.
Note
Unless otherwise stated recognized readable MAC Address formats include only the following formats:
IEEE 802 format for printing EUI-48 and MAC-48 addresses in six groups of two hexadecimal digits, separated by a dash (
-). E.g.AA-BB-CC-DD-EE-FFCommon Six groups of two hexadecimal digits separated by colons (
:). E.g.AA:BB:CC:DD:EE:FFSix groups of two hexadecimal digits separated by a space character. E.g.
AA BB CC DD EE FF12 hexadecimal digits with no delimitation. E.g.
AABBCCDDEEFFCisco three groups of four hexadecimal digits separated by dots (
.). E.g.AABB.CCDD.EEFF
For the sake of parsing and reading these formats are case insensitive.
Structure of a MAC-48 Address¶
Creation¶
Constructor¶
IEnumerable<byte>¶
A new MacAddress may be constructed by providing an IEnumerable<byte> of six bytes to the constructor.
public MacAddress(IEnumerable<byte> bytes)
Factory¶
Parse string¶
A MacAddress may also be created via either the Parse or safe TryParse method. Not that these methods are strict in that they will only succeed with a MAC address in a known format. If you wish to more liberally parse a string into a MacAddress see the ParseAny and TryParseAny defined below.
public static MacAddress Parse(string input)
public static bool TryParse(string input, out MacAddress macAddress)
ParseAny string¶
ParseAny and the safe TryParseAny allow the parsing of an arbitrary string that may be a Mac address into a MacAddress. It looks for six hexadecimal digits within the string, joins them and interprets the result as consecutive big-endian hextets. If six, and only six, hexadecimal digits are not found the parse will fail.
public static MacAddress ParseAny(string input)
public static bool TryParseAny(string input, out MacAddress macAddress)
Functionality¶
Properties¶
boolIsDefaultreturns
trueif, and only if, the MAC Address is the EUI-48 default 2, meaning all bits of the MAC Address are set making it equivalent toFF:FF:FF:FF:FF:FF.boolIsGloballyUniquereturns
trueif, and only if, is globally unique (OUI 4 enforced).boolIsLocallyAdministeredreturns
trueif, and only if, is locally administered.boolIsMulticastreturns
trueif, and only if, the MAC Address is multicast.boolIsUnicastreturns
trueif, and only if, the MAC Address is unicast.boolIsUnusablereturns
trueif, and only if, the MAC Address is “unusable”, meaning all OUI bits of the MAC Address are unset.MacAddressDefaultMacAddressProvides a
MacAddressthat represents the default ornullcase MAC address.RegexAllFormatMacAddressRegularExpressionReturns a regular expression for matching accepted MAC Address formats.
RegexCommonFormatMacAddressRegularExpressionReturns a regular expression for matching the “common” six groups of two uppercase hexadecimal digits format.
stringAllFormatMacAddressPatternReturns a regular expression pattern for matching accepted MAC Address formats.
stringCommonFormatMacAddressPatternReturns a regular expression pattern for matching the “common” six groups of two uppercase hexadecimal digits format.
Methods¶
GetAddressBytes¶
GetAddressBytes returns a copy of the underlying big-endian bytes of the MacAddress. This will always be six bytes in length.
public byte[] GetAddressBytes()
IFormatable¶
MacAddress offers a number or preexisting formats that are accessible via the standard ToString method provided by IFormattable interface.
Format |
Name |
Description |
Example |
|---|---|---|---|
|
General Format |
Uppercase hexadecimal encoded bytes separated by colons |
|
|
Uppercase Hexadecimal |
Contiguous uppercase hexadecimal digits |
|
|
Lowercase Hexadecimal |
Contiguous lowerrcase hexadecimal digits |
|
|
Cisco |
Three groups of four uppercase hexadecimal digits separated by periods |
|
|
Space delimited hextets |
Hexadecimal bytes separated by a space character |
|
|
IEEE 802 |
Hexadecimal encoded bytes separated by a dash characte |
|
|
Integer |
Big-endian integer value |
|
Operators¶
MacAddress implements all the standard C# equality and comparison operators. The comparison operators treat the MacAddress bytes as an unsigned big-endian integer value.
Footnotes
- 1
48-Bit MAC is a Media Access Control Address (MAC) following both the now deprecated MAC-48 and the active EUI-48 specifications.
- 2
The recommended null or default value for EUI-48 is
FF-FF-FF-FF-FF-FF- 3
- 4(1,2)
Organizationally Unique Identifier (OUI) is the first 3-bytes (24-bits) of a MAC-48 MAC Address.
- 5
Company Id (Cid) is the last 3-bytes (24-bits) of a MAC-48 MAC Address.
- 6
Usable EUI-48 values are based on a zeroed OUI. A all zero EUI value, such as
00-00-00-00-00-00and00-00-00-FA-BC-21, according to spec shall not be used as an identifier.