Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7f4056d
updating for Try .NET
WilliamAntonRohm Oct 18, 2019
2ce654b
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/cto…
WilliamAntonRohm Oct 21, 2019
c967211
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/cto…
WilliamAntonRohm Oct 21, 2019
38214c0
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/em…
WilliamAntonRohm Oct 21, 2019
c57ea4f
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/pa…
WilliamAntonRohm Oct 21, 2019
d9799fb
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/pa…
WilliamAntonRohm Oct 21, 2019
5e14ce1
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tr…
WilliamAntonRohm Oct 21, 2019
5f1ae2d
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray…
WilliamAntonRohm Oct 21, 2019
3e2739d
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray…
WilliamAntonRohm Oct 21, 2019
65bd20a
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray…
WilliamAntonRohm Oct 21, 2019
cac4f58
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/cto…
WilliamAntonRohm Oct 22, 2019
393e1fe
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/cto…
WilliamAntonRohm Oct 22, 2019
0657640
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/em…
WilliamAntonRohm Oct 22, 2019
453e8ad
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/em…
WilliamAntonRohm Oct 22, 2019
5d83c7c
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/em…
WilliamAntonRohm Oct 22, 2019
2ce696a
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.empty/cs/em…
WilliamAntonRohm Oct 22, 2019
db34025
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tr…
WilliamAntonRohm Oct 22, 2019
73e37b9
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tr…
WilliamAntonRohm Oct 22, 2019
228f020
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray…
WilliamAntonRohm Oct 22, 2019
ff9a634
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray…
WilliamAntonRohm Oct 22, 2019
b3081ad
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.tobytearray…
WilliamAntonRohm Oct 22, 2019
d06ec1f
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.newguid/cs/…
WilliamAntonRohm Oct 22, 2019
d7c3bb5
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.newguid/cs/…
WilliamAntonRohm Oct 22, 2019
9ca7a03
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/pa…
WilliamAntonRohm Oct 22, 2019
7557781
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/pa…
WilliamAntonRohm Oct 22, 2019
08cd71d
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tr…
WilliamAntonRohm Oct 22, 2019
88d15a8
Update snippets/csharp/VS_Snippets_CLR_System/system.guid.parse/cs/tr…
WilliamAntonRohm Oct 22, 2019
2add2af
per review comments
WilliamAntonRohm Oct 22, 2019
9068c59
per review comments
WilliamAntonRohm Oct 22, 2019
97a26c1
per review comments
WilliamAntonRohm Oct 22, 2019
1e6fcc0
per review comments
WilliamAntonRohm Oct 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions snippets/csharp/VS_Snippets_CLR_System/system.guid.ctor/cs/ctor1.cs
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,11 +1,11 @@
// <Snippet3>
using System;
using System;

public class Example
{
public static void Main()
{
Guid originalGuid = Guid.NewGuid();
// <Snippet3>
var originalGuid = Guid.NewGuid();
// Create an array of string representations of the GUID.
string[] stringGuids = { originalGuid.ToString("B"),
originalGuid.ToString("D"),
Expand All @@ -14,20 +14,22 @@ public static void Main()
// Parse each string representation.
foreach (var stringGuid in stringGuids) {
try {
Guid newGuid = Guid.Parse(stringGuid);
Console.WriteLine("Converted {0} to a Guid", stringGuid);
var newGuid = Guid.Parse(stringGuid);
Console.WriteLine($"Converted {stringGuid} to a Guid");
}
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:
//
// 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
// </Snippet3>
}
}
// 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
// </Snippet3>
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>
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))
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also seeing some occurrences of var which would compile and be correct C# code, but doesn't follow the guidelines, which says to use var when the type is clear from the right hand side.

Copy link
Member

Choose a reason for hiding this comment

The 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 that doesn't follow the guidelines like:

var g = Guid.NewGuid();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Youssef1313

var g = Guid.NewGuid();

To me, that example is clear. I would be be very surprised if a method Guid.NewGuid() returned anything other than a Guid. When I reviewed the changes in this PR, I didn't find uses of var that were unclear. I don't think another PR is needed here.

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>
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>