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
boolIsDefault:returns
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.boolIsGloballyUnique:returns
trueif, and only if, is globally unique (OUI [4] enforced).boolIsLocallyAdministered:returns
trueif, and only if, is locally administered.boolIsMulticast:returns
trueif, and only if, the MAC Address is multicast.boolIsUnicast:returns
trueif, and only if, the MAC Address is unicast.boolIsUnusable:returns
trueif, and only if, the MAC Address is “unusable” [6], meaning all OUI bits of the MAC Address are unset.MacAddressDefaultMacAddress:Provides a
MacAddressthat represents the default ornullcase MAC address.RegexAllFormatMacAddressRegularExpression:Returns a regular expression for matching accepted MAC Address formats.
RegexCommonFormatMacAddressRegularExpression:Returns a regular expression for matching the “common” six groups of two uppercase hexadecimal digits format.
stringAllFormatMacAddressPattern:Returns a regular expression pattern for matching accepted MAC Address formats.
stringCommonFormatMacAddressPattern:Returns 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()
GetOuiBytes
GetOuiBytes returns the Organizationally Unique Identifier (OUI) [4] of the MacAddress.
public byte[] GetOuiBytes()
GetCidBytes
GetCidBytes returns the Company ID (CID) [5] of the MacAddress.
public byte[] GetCidBytes()
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