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-FF
Common Six groups of two hexadecimal digits separated by colons (
:
). E.g.AA:BB:CC:DD:EE:FF
Six groups of two hexadecimal digits separated by a space character. E.g.
AA BB CC DD EE FF
12 hexadecimal digits with no delimitation. E.g.
AABBCCDDEEFF
Cisco 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.
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¶
bool
IsDefaultreturns
true
if, 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
.bool
IsGloballyUniquereturns
true
if, and only if, is globally unique (OUI 4 enforced).bool
IsLocallyAdministeredreturns
true
if, and only if, is locally administered.bool
IsMulticastreturns
true
if, and only if, the MAC Address is multicast.bool
IsUnicastreturns
true
if, and only if, the MAC Address is unicast.bool
IsUnusablereturns
true
if, and only if, the MAC Address is “unusable”, meaning all OUI bits of the MAC Address are unset.MacAddress
DefaultMacAddressProvides a
MacAddress
that represents the default ornull
case MAC address.Regex
AllFormatMacAddressRegularExpressionReturns a regular expression for matching accepted MAC Address formats.
Regex
CommonFormatMacAddressRegularExpressionReturns a regular expression for matching the “common” six groups of two uppercase hexadecimal digits format.
string
AllFormatMacAddressPatternReturns a regular expression pattern for matching accepted MAC Address formats.
string
CommonFormatMacAddressPatternReturns 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-00
and00-00-00-FA-BC-21
, according to spec shall not be used as an identifier.