-
Notifications
You must be signed in to change notification settings - Fork 5.1k
updating Guid.xml samples for Try .NET #1654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7f4056d
2ce654b
c967211
38214c0
c57ea4f
d9799fb
5e14ce1
5f1ae2d
3e2739d
65bd20a
cac4f58
393e1fe
0657640
453e8ad
5d83c7c
2ce696a
db34025
73e37b9
228f020
ff9a634
b3081ad
d06ec1f
d7c3bb5
9ca7a03
7557781
08cd71d
88d15a8
2add2af
9068c59
97a26c1
1e6fcc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
// <Snippet1> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
// <Snippet1> | ||
string[] guidStrings = { "ca761232ed4211cebacd00aa0057b223", | ||
"CA761232-ED42-11CE-BACD-00AA0057B223", | ||
"{CA761232-ED42-11CE-BACD-00AA0057B223}", | ||
"(CA761232-ED42-11CE-BACD-00AA0057B223)", | ||
"{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" }; | ||
foreach (var guidString in guidStrings) { | ||
Guid guid = new Guid(guidString); | ||
Console.WriteLine("Original string: {0}", guidString); | ||
Console.WriteLine("Guid: {0}", guid); | ||
Console.WriteLine($"Original string: {guidString}"); | ||
Console.WriteLine($"Guid: {guid}"); | ||
Console.WriteLine(); | ||
} | ||
|
||
// The example displays the following output: | ||
// Original string: ca761232ed4211cebacd00aa0057b223 | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: CA761232-ED42-11CE-BACD-00AA0057B223 | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: {CA761232-ED42-11CE-BACD-00AA0057B223} | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: (CA761232-ED42-11CE-BACD-00AA0057B223) | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}} | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// </Snippet1> | ||
} | ||
} | ||
// The example displays the following output: | ||
// Original string: ca761232ed4211cebacd00aa0057b223 | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: CA761232-ED42-11CE-BACD-00AA0057B223 | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: {CA761232-ED42-11CE-BACD-00AA0057B223} | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: (CA761232-ED42-11CE-BACD-00AA0057B223) | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// | ||
// Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}} | ||
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 | ||
// </Snippet1> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
// <Snippet2> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
Guid g = new Guid(0xA, 0xB, 0xC, | ||
// <Snippet2> | ||
var g = new Guid(0xA, 0xB, 0xC, | ||
new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7 } ); | ||
Console.WriteLine("{0:B}", g); | ||
Console.WriteLine($"{g:B}"); | ||
|
||
// The example displays the following output: | ||
// {0000000a-000b-000c-0001-020304050607} | ||
// </Snippet2> | ||
} | ||
} | ||
// The example displays the following output: | ||
// {0000000a-000b-000c-0001-020304050607} | ||
// </Snippet2> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
// <Snippet1> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
// <Snippet1> | ||
// Create a GUID and determine whether it consists of all zeros. | ||
Guid guid1 = Guid.NewGuid(); | ||
var guid1 = Guid.NewGuid(); | ||
Console.WriteLine(guid1); | ||
Console.WriteLine("Empty: {0}\n", guid1 == Guid.Empty); | ||
Console.WriteLine($"Empty: {guid1 == Guid.Empty}\n"); | ||
|
||
// Create a GUID with all zeros and compare it to Empty. | ||
Byte[] bytes = new Byte[16]; | ||
Guid guid2 = new Guid(bytes); | ||
var bytes = new Byte[16]; | ||
var guid2 = new Guid(bytes); | ||
Console.WriteLine(guid2); | ||
Console.WriteLine("Empty: {0}", guid2 == Guid.Empty); | ||
Console.WriteLine($"Empty: {guid2 == Guid.Empty}"); | ||
|
||
// The example displays output like the following: | ||
// 11c43ee8-b9d3-4e51-b73f-bd9dda66e29c | ||
// Empty: False | ||
// | ||
// 00000000-0000-0000-0000-000000000000 | ||
// Empty: True | ||
// </Snippet1> | ||
} | ||
} | ||
// The example displays output like the following: | ||
// 11c43ee8-b9d3-4e51-b73f-bd9dda66e29c | ||
// Empty: False | ||
// | ||
// 00000000-0000-0000-0000-000000000000 | ||
// Empty: True | ||
// </Snippet1> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
//<snippet1> | ||
// This code example demonstrates the Guid.NewGuid() method. | ||
// This code example demonstrates the Guid.NewGuid() method. | ||
|
||
using System; | ||
|
||
class Sample | ||
{ | ||
public static void Main() | ||
{ | ||
Guid g; | ||
//<snippet1> | ||
// Create and display the value of two GUIDs. | ||
g = Guid.NewGuid(); | ||
var g = Guid.NewGuid(); | ||
Console.WriteLine(g); | ||
Console.WriteLine(Guid.NewGuid()); | ||
|
||
// This code example produces a result similar to the following: | ||
|
||
// 0f8fad5b-d9cb-469f-a165-70867728950e | ||
// 7c9e6679-7425-40de-944b-e07fc1f90ae7 | ||
//</snippet1> | ||
} | ||
} | ||
|
||
/* | ||
This code example produces the following results: | ||
|
||
0f8fad5b-d9cb-469f-a165-70867728950e | ||
7c9e6679-7425-40de-944b-e07fc1f90ae7 | ||
|
||
*/ | ||
//</snippet1> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
// <Snippet4> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
// <Snippet4> | ||
// Define an array of all format specifiers. | ||
string[] formats = { "N", "D", "B", "P", "X" }; | ||
Guid guid = Guid.NewGuid(); | ||
var guid = Guid.NewGuid(); | ||
// Create an array of valid Guid string representations. | ||
string[] stringGuids = new string[formats.Length]; | ||
var stringGuids = new string[formats.Length]; | ||
for (int ctr = 0; ctr < formats.Length; ctr++) | ||
stringGuids[ctr] = guid.ToString(formats[ctr]); | ||
|
||
// Parse the strings in the array using the "B" format specifier. | ||
foreach (var stringGuid in stringGuids) { | ||
try { | ||
Guid newGuid = Guid.ParseExact(stringGuid, "B"); | ||
Console.WriteLine("Successfully parsed {0}", stringGuid); | ||
var newGuid = Guid.ParseExact(stringGuid, "B"); | ||
Console.WriteLine($"Successfully parsed {stringGuid}"); | ||
} | ||
catch (ArgumentNullException) { | ||
Console.WriteLine("The string to be parsed is null."); | ||
WilliamAntonRohm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
catch (FormatException) { | ||
Console.WriteLine("Bad Format: {0}", stringGuid); | ||
Console.WriteLine($"Bad Format: {stringGuid}"); | ||
} | ||
} | ||
|
||
// The example displays output similar to the following: | ||
// | ||
// Bad Format: eb5c8c7d187a44e68afb81e854c39457 | ||
// Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457 | ||
// Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457} | ||
// Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457) | ||
// Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}} | ||
// </Snippet4> | ||
} | ||
} | ||
// The example displays the following output: | ||
// Bad Format: eb5c8c7d187a44e68afb81e854c39457 | ||
// Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457 | ||
// Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457} | ||
// Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457) | ||
// Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}} | ||
// </Snippet4> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
// <Snippet2> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
Guid originalGuid = Guid.NewGuid(); | ||
// <Snippet2> | ||
var originalGuid = Guid.NewGuid(); | ||
// Create an array of string representations of the GUID. | ||
string[] stringGuids = { originalGuid.ToString("B"), | ||
originalGuid.ToString("D"), | ||
originalGuid.ToString("X") }; | ||
|
||
// Parse each string representation. | ||
Guid newGuid; | ||
foreach (var stringGuid in stringGuids) { | ||
if (Guid.TryParse(stringGuid, out newGuid)) | ||
Console.WriteLine("Converted {0} to a Guid", stringGuid); | ||
if (Guid.TryParse(stringGuid, out var newGuid)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this change is incorrect. var is used when declaring variables. However, I still didn't test this in an IDE. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did test this on https://try.dot.net/. Check this: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#out-variables I'd use discards since that variable is not used but try.net didn't like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also seeing some occurrences of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mairaw, Thanks, didn't know this feature of C# 7. I'll open another PR for the other occurrences of var g = Guid.NewGuid(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var g = Guid.NewGuid(); To me, that example is clear. I would be be very surprised if a method |
||
Console.WriteLine($"Converted {stringGuid} to a Guid"); | ||
else | ||
Console.WriteLine("Unable to convert {0} to a Guid", | ||
stringGuid); | ||
Console.WriteLine($"Unable to convert {stringGuid} to a Guid"); | ||
} | ||
|
||
WilliamAntonRohm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The example displays output similar to the following: | ||
// | ||
// Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid | ||
// Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid | ||
// Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid | ||
// </Snippet2> | ||
} | ||
} | ||
// The example displays the following output: | ||
// Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid | ||
// Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid | ||
// Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid | ||
// </Snippet2> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
// <Snippet5> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
// <Snippet5> | ||
// Define an array of all format specifiers. | ||
string[] formats = { "N", "D", "B", "P", "X" }; | ||
Guid guid = Guid.NewGuid(); | ||
var guid = Guid.NewGuid(); | ||
// Create an array of valid Guid string representations. | ||
string[] stringGuids = new string[formats.Length]; | ||
var stringGuids = new string[formats.Length]; | ||
for (int ctr = 0; ctr < formats.Length; ctr++) | ||
stringGuids[ctr] = guid.ToString(formats[ctr]); | ||
|
||
// Parse the strings in the array using the "B" format specifier. | ||
foreach (var stringGuid in stringGuids) { | ||
Guid newGuid; | ||
if (Guid.TryParseExact(stringGuid, "B", out newGuid)) | ||
Console.WriteLine("Successfully parsed {0}", stringGuid); | ||
if (Guid.TryParseExact(stringGuid, "B", out var newGuid)) | ||
Console.WriteLine($"Successfully parsed {stringGuid}"); | ||
else | ||
Console.WriteLine("Unable to parse '{0}'", stringGuid); | ||
Console.WriteLine($"Unable to parse '{stringGuid}'"); | ||
} | ||
|
||
// The example displays output similar to the following: | ||
// | ||
// Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a' | ||
// Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a' | ||
// Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a} | ||
// Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)' | ||
// Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}' | ||
// </Snippet5> | ||
} | ||
} | ||
// The example displays the following output: | ||
// Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a' | ||
// Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a' | ||
// Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a} | ||
// Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)' | ||
// Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}' | ||
// </Snippet5> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
// <Snippet1> | ||
using System; | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
Guid guid = Guid.NewGuid(); | ||
Console.WriteLine("Guid: {0}", guid); | ||
Byte[] bytes = guid.ToByteArray(); | ||
// <Snippet1> | ||
var guid = Guid.NewGuid(); | ||
Console.WriteLine($"Guid: {guid}"); | ||
var bytes = guid.ToByteArray(); | ||
foreach (var byt in bytes) | ||
Console.Write("{0:X2} ", byt); | ||
Console.Write($"{byt:X2} "); | ||
|
||
Console.WriteLine(); | ||
Guid guid2 = new Guid(bytes); | ||
Console.WriteLine("Guid: {0} (Same as First Guid: {1})", guid2, guid2.Equals(guid)); | ||
var guid2 = new Guid(bytes); | ||
Console.WriteLine($"Guid: {guid2} (Same as First Guid: {guid2.Equals(guid)})"); | ||
|
||
// The example displays output similar to the following: | ||
// | ||
// Guid: 35918bc9-196d-40ea-9779-889d79b753f0 | ||
// C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 | ||
// Guid: 35918bc9-196d-40ea-9779-889d79b753f0 (Same as First Guid: True) | ||
// </Snippet1> | ||
} | ||
} | ||
// The example displays the following output: | ||
// Guid: 35918bc9-196d-40ea-9779-889d79b753f0 | ||
// C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 | ||
// Guid: 35918bc9-196d-40ea-9779-889d79b753f0 (Same as First Guid: True) | ||
// </Snippet1> |
Uh oh!
There was an error while loading. Please reload this page.