Skip to content

Commit

Permalink
ArgumentOutOfRangeException code example modernization (#7346)
Browse files Browse the repository at this point in the history
  • Loading branch information
CommonLoon102 authored Nov 5, 2021
1 parent 85814d8 commit 8a20dff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
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>
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>

0 comments on commit 8a20dff

Please sign in to comment.