When guid are used their value tends not to change, throughout it's usage.
If we make it a compile-time constant,
Guid.Parse currently supports 5 different formats.
| Specifier | Description | Format |
| --- | --- | --- |
| N | 32 digits | 00000000000000000000000000000000 |
| D | 32 digits seperated by hypens | 00000000-0000-0000-0000-0000000000000 |
| B | 32 digits seperated by hypens, enclosed in braces | {00000000-0000-0000-0000-0000000000000} |
| P | 32 digits seperated by hypens, enclosed in parentheses | (00000000-0000-0000-0000-0000000000000) |
| X | Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces | {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} |
These format should also be supported by any GUID literal.
Proposed Literal Syntax (VB)
We could extend VB's date time literal syntax #2016/06/01# to cover GUIDs.
``` vb.net
Dim guid0 As Guid = #00000000000000000000000000000000# ' Specifier N
Dim guid1 As Guid = #00000000-0000-0000-0000-0000000000000# ' Specifier D
Dim guid2 As Guid = #{00000000-0000-0000-0000-0000000000000}# ' Specifier B
Dim guid3 As Guid = #(00000000-0000-0000-0000-0000000000000)# ' Specified P
Dim guid4 As Guid = #{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}#
In an Attribute
``` vb
Imports System
Imports System.Runtime.InteropServices
<GuidAttribute(#9ED54F84-A89D-4fcd-A854-44251E925F09#)> _
Public Class SampleClass
' Insert class members here.
End Class
Missing an opening curly brace in last example.
Not to sound entirely daft; however, what advantage would this bring other than a very rare convenience? Most people generate GUIDs using the Create GUID tool, which already has everything you need right there. In the case where you're referencing existing GUIDs the source you get them from will likely already have them in the proper format for you.
The notion of 'well known' is unclear since the use of GUIDs is entirely contextual. They mean whatever you want them to mean, in the case of COM what would you define as 'well known'? In a typical scenario you're likely to use the GUID itself once, defining it as a local of some sort that is referenced elsewhere.
More useful than a GUID constant and associated intelli-sense would be an authoritative source (i.e. from Microsoft) for these COM interfaces that aren't well documented that dig deep into the innards of how, for instance, the MFC operates in a managed library of sorts.
Related issue: #1387
It can be used in attributes.
How come? Decimal and DateTime have their own literals, but can't be used in attributes. What would be the metadata representation?
Removes the runtime parse of the GUID string.
If you want efficiency, you can use the Guid(int, short, short, byte, byte, byte, byte, byte, byte, byte, byte) constructor, which is effectively equivalent to the X form.
@AlexanderMorou
what advantage would this bring other than a very rare convenience?
Compile-time format checking. GUIDs are long and easy to mess up when manually typing them. Compiler support could help prevent mistakes.
I you just want compile time checking, I suppose you could write an analyzer for that.
I kind of like the idea of guid literals. Just throwing out there the use of a different prefix/suffix of G instead of #.
As with any addition to the language; clearly more thought should go into it... but my vote right now is that "let's chew on it a bit".
Most helpful comment
Not to sound entirely daft; however, what advantage would this bring other than a very rare convenience? Most people generate GUIDs using the Create GUID tool, which already has everything you need right there. In the case where you're referencing existing GUIDs the source you get them from will likely already have them in the proper format for you.
The notion of 'well known' is unclear since the use of GUIDs is entirely contextual. They mean whatever you want them to mean, in the case of COM what would you define as 'well known'? In a typical scenario you're likely to use the GUID itself once, defining it as a local of some sort that is referenced elsewhere.
More useful than a GUID constant and associated intelli-sense would be an authoritative source (i.e. from Microsoft) for these COM interfaces that aren't well documented that dig deep into the innards of how, for instance, the MFC operates in a managed library of sorts.