-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArgumentOutOfRangeException code example modernization (#7346)
- Loading branch information
1 parent
85814d8
commit 8a20dff
Showing
2 changed files
with
29 additions
and
23 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
samples/snippets/csharp/VS_Snippets_CLR/ArgumentOutOfRangeException/CS/cs.csproj
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
44 changes: 21 additions & 23 deletions
44
samples/snippets/csharp/VS_Snippets_CLR/ArgumentOutOfRangeException/CS/program.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,43 +1,41 @@ | ||
// <Snippet1> | ||
using System; | ||
using static System.Console; | ||
|
||
class Program | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
public static void Main(string[] args) | ||
{ | ||
try | ||
{ | ||
Guest guest1 = new Guest("Ben", "Miller", 17); | ||
Console.WriteLine(guest1.GuestInfo()); | ||
var guest1 = new Guest("Ben", "Miller", 17); | ||
WriteLine(guest1.GuestInfo); | ||
} | ||
catch (ArgumentOutOfRangeException outOfRange) | ||
catch (ArgumentOutOfRangeException argumentOutOfRangeException) | ||
{ | ||
|
||
Console.WriteLine("Error: {0}", outOfRange.Message); | ||
WriteLine($"Error: {argumentOutOfRangeException.Message}"); | ||
} | ||
} | ||
} | ||
|
||
class Guest | ||
{ | ||
private string FirstName; | ||
private string LastName; | ||
private int Age; | ||
private const int minimumRequiredAge = 21; | ||
|
||
public Guest(string fName, string lName, int age) | ||
{ | ||
FirstName = fName; | ||
LastName = lName; | ||
if (age < 21) | ||
throw new ArgumentOutOfRangeException("age","All guests must be 21-years-old or older."); | ||
else | ||
Age = age; | ||
} | ||
private string firstName; | ||
private string lastName; | ||
private int age; | ||
|
||
public string GuestInfo() | ||
public Guest(string firstName, string lastName, int age) | ||
{ | ||
string gInfo = FirstName + " " + LastName + ", " + Age.ToString(); | ||
return(gInfo); | ||
if (age < minimumRequiredAge) | ||
throw new ArgumentOutOfRangeException(nameof(age), $"All guests must be {minimumRequiredAge}-years-old or older."); | ||
|
||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
this.age = age; | ||
} | ||
|
||
public string GuestInfo => $"{firstName} {lastName}, {age}"; | ||
} | ||
// </Snippet1> | ||
// </Snippet1> |