Skip to content

fix: update Program.cs to use top-level statements for vs-2022 #10350

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
260 changes: 127 additions & 133 deletions docs/get-started/csharp/tutorial-console.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,89 +606,85 @@ Let's get started.

```

1. Also add a new `Program` class, as follows:
1. Also add following codes to `Program` class:

```csharp
class Program
bool endApp = false;
// Display title as the C# console calculator app.
WriteLine("Console Calculator in C#\r");
WriteLine("------------------------\n");

while (!endApp)
{
static void Main(string[] args)
// Declare variables and set to empty.
string? numInput1 = "";
string? numInput2 = "";
double result = 0;

// Ask the user to type the first number.
Write("Type a number, and then press Enter: ");
numInput1 = ReadLine();

double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
bool endApp = false;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

while (!endApp)
Write("This is not valid input. Please enter a numeric value: ");
numInput1 = ReadLine();
}

// Ask the user to type the second number.
Write("Type another number, and then press Enter: ");
numInput2 = ReadLine();

double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Write("This is not valid input. Please enter a numeric value: ");
numInput2 = ReadLine();
}

// Ask the user to choose an operator.
WriteLine("Choose an operator from the following list:");
WriteLine("\ta - Add");
WriteLine("\ts - Subtract");
WriteLine("\tm - Multiply");
WriteLine("\td - Divide");
Write("Your option? ");

string? op = ReadLine();

// Validate input is not null, and matches the pattern
if (op == null || !Regex.IsMatch(op, "[a|s|m|d]"))
{
WriteLine("Error: Unrecognized input.");
}
else
{
try
{
// Declare variables and set to empty.
// Use Nullable types (with ?) to match type of System.Console.ReadLine
string? numInput1 = "";
string? numInput2 = "";
double result = 0;

// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();

double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput1 = Console.ReadLine();
}

// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();

double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput2 = Console.ReadLine();
}

// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");

string? op = Console.ReadLine();

// Validate input is not null, and matches the pattern
if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]"))
result = Calculator.Common.Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("Error: Unrecognized input.");
WriteLine("This operation will result in a mathematical error.\n");
}
else
{
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
{
WriteLine("Your result: {0:0.##}\n", result);
}
Console.WriteLine("------------------------\n");

// Wait for the user to respond before closing.
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;

Console.WriteLine("\n"); // Friendly linespacing.
}
return;
catch (Exception e)
{
WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
}

WriteLine("------------------------\n");

// Wait for the user to respond before closing.
Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (ReadLine() == "n") endApp = true;

WriteLine("\n"); // Friendly linespacing.
}
```

Expand Down Expand Up @@ -876,83 +872,81 @@ class Calculator

class Program
{
static void Main(string[] args)
bool endApp = false;

// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

while (!endApp)
{
bool endApp = false;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
// Declare variables and set to empty.
// Use Nullable types (with ?) to match type of System.Console.ReadLine
string? numInput1 = "";
string? numInput2 = "";
double result = 0;

while (!endApp)
{
// Declare variables and set to empty.
// Use Nullable types (with ?) to match type of System.Console.ReadLine
string? numInput1 = "";
string? numInput2 = "";
double result = 0;
// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();

// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter: ");
double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput1 = Console.ReadLine();
}

double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput1 = Console.ReadLine();
}
// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();

// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput2 = Console.ReadLine();
}

double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput2 = Console.ReadLine();
}

// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");

string? op = Console.ReadLine();
string? op = Console.ReadLine();

// Validate input is not null, and matches the pattern
if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]"))
// Validate input is not null, and matches the pattern
if (op == null || !Regex.IsMatch(op, "[a|s|m|d]"))
{
Console.WriteLine("Error: Unrecognized input.");
}
else
{
try
{
Console.WriteLine("Error: Unrecognized input.");
}
else
{
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
Console.WriteLine("------------------------\n");
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
}

// Wait for the user to respond before closing.
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;
Console.WriteLine("------------------------\n");

Console.WriteLine("\n"); // Friendly linespacing.
}
return;
// Wait for the user to respond before closing.
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;

Console.WriteLine("\n"); // Friendly linespacing.
}
}
```
Expand Down