forked from dotnet/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating Guid.xml samples for Try .NET (dotnet#1654)
* updating for Try .NET * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor1.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor2.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/empty.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/parseex1.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/parseexactex1.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseexactex1.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs Co-Authored-By: Youssef Victor <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor1.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor2.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/empty.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/empty.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/empty.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/empty.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseexactex1.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseexactex1.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.newguid/cs/ng.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.newguid/cs/ng.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/parseex1.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/parseexactex1.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseex1.cs Co-Authored-By: Maira Wenzel <[email protected]> * Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseexactex1.cs Co-Authored-By: Maira Wenzel <[email protected]> * per review comments * per review comments * per review comments * per review comments
- Loading branch information
1 parent
d71c49c
commit 7b12611
Showing
9 changed files
with
126 additions
and
120 deletions.
There are no files selected for viewing
41 changes: 21 additions & 20 deletions
41
snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
15 changes: 8 additions & 7 deletions
15
snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
29 changes: 15 additions & 14 deletions
29
snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/empty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
22 changes: 9 additions & 13 deletions
22
snippets/csharp/VS_Snippets_CLR_System/system.guid.newguid/cs/ng.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 16 additions & 14 deletions
30
snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/parseexactex1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
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> |
26 changes: 13 additions & 13 deletions
26
snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseex1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
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"); | ||
} | ||
|
||
// 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> |
31 changes: 16 additions & 15 deletions
31
snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tryparseexactex1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
28 changes: 15 additions & 13 deletions
28
snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray/cs/tobytearray3.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |