Skip to content

Commit

Permalink
Finish getstarted tutorials (dotnet#4330)
Browse files Browse the repository at this point in the history
* upgrade to .NET 5

Top level statements,
new TFM

* update to .NET 5

Add top level statements
Add missing samples from article.

* update to .NET 5

Use top level statements for simplicity
  • Loading branch information
BillWagner authored Feb 16, 2021
1 parent a994004 commit a39cf54
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 232 deletions.
3 changes: 2 additions & 1 deletion csharp/branches-quickstart/BranchesAndLoops.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<nullable>enable</nullable>
</PropertyGroup>

</Project>
150 changes: 82 additions & 68 deletions csharp/branches-quickstart/Program.cs
Original file line number Diff line number Diff line change
@@ -1,83 +1,97 @@
using System;

namespace BranchesAndLoops
ExploreIf();

ExploreLoops();

ChallengeAnswer();

void ExploreIf()
{
class Program
int a = 5;
int b = 3;
if (a + b > 10)
{
static void ExploreIf()
{
int a = 5;
int b = 3;
if (a + b > 10)
{
Console.WriteLine("The answer is greater than 10");
}
else
{
Console.WriteLine("The answer is not greater than 10");
}
Console.WriteLine("The answer is greater than 10");
}
else
{
Console.WriteLine("The answer is not greater than 10");
}

int c = 4;
if ((a + b + c > 10) && (a > b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("And the first number is greater than the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("Or the first number is not greater than the second");
}
int c = 4;
if ((a + b + c > 10) && (a > b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("And the first number is greater than the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("Or the first number is not greater than the second");
}

if ((a + b + c > 10) || (a > b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("Or the first number is greater than the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("And the first number is not greater than the second");
}
}
if ((a + b + c > 10) || (a > b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("Or the first number is greater than the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("And the first number is not greater than the second");
}
}

static void ChallengeAnswer()
{
int sum = 0;
for (int number = 1; number < 21; number++)
{
if (number % 3 == 0)
{
sum = sum + number;
}
}
Console.WriteLine($"The sum is {sum}");
}
void ExploreLoops()
{
int counter = 0;
while (counter < 10)
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
}

static void Main(string[] args)
{
ExploreIf();
counter = 0;
do
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
} while (counter < 10);

int counter = 0;
while (counter < 10)
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
}
for (int index = 0; index < 10; index++)
{
Console.WriteLine($"Hello World! The index is {index}");
}

counter = 0;
do
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
} while (counter < 10);
for (int row = 1; row < 11; row++)
{
Console.WriteLine($"The row is {row}");
}
for (char column = 'a'; column < 'k'; column++)
{
Console.WriteLine($"The column is {column}");
}

for (int index = 0; index < 10; index++)
{
Console.WriteLine($"Hello World! The index is {index}");
}
for (int row = 1; row < 11; row++)
{
for (char column = 'a'; column < 'k'; column++)
{
Console.WriteLine($"The cell is ({row}, {column})");
}
}
}

ChallengeAnswer();
void ChallengeAnswer()
{
int sum = 0;
for (int number = 1; number < 21; number++)
{
if (number % 3 == 0)
{
sum = sum + number;
}
}
Console.WriteLine($"The sum is {sum}");
}

114 changes: 59 additions & 55 deletions csharp/list-quickstart/Program.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
using System;
using System.Collections.Generic;

namespace list_quickstart
WorkWithString();

var fibonacciNumbers = new List<int> { 1, 1 };

while (fibonacciNumbers.Count < 20)
{
class Program
var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];
var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];

fibonacciNumbers.Add(previous + previous2);
}
foreach (var item in fibonacciNumbers)
Console.WriteLine(item);

void WorkWithString()
{
var names = new List<string> { "<name>", "Ana", "Felipe" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

Console.WriteLine();
names.Add("Maria");
names.Add("Bill");
names.Remove("Ana");
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

Console.WriteLine($"My name is {names[0]}");
Console.WriteLine($"I've added {names[2]} and {names[3]} to the list");

Console.WriteLine($"The list has {names.Count} people in it");

var index = names.IndexOf("Felipe");
if (index == -1)
{
Console.WriteLine($"When an item is not found, IndexOf returns {index}");
}
else
{
Console.WriteLine($"The name {names[index]} is at index {index}");
}

index = names.IndexOf("Not Found");
if (index == -1)
{
Console.WriteLine($"When an item is not found, IndexOf returns {index}");
}
else
{
Console.WriteLine($"The name {names[index]} is at index {index}");

}

names.Sort();
foreach (var name in names)
{
#region WorkingWithStrings
static void Main(string[] args)
{
WorkingWithStrings();

var fibonacciNumbers = new List<int> { 1, 1 };

while (fibonacciNumbers.Count < 20)
{
var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];
var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];

fibonacciNumbers.Add(previous + previous2);
}
foreach (var item in fibonacciNumbers)
Console.WriteLine(item);
}

public static void WorkingWithStrings()
{
var names = new List<string> { "<name>", "Ana", "Felipe" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

Console.WriteLine();
names.Add("Maria");
names.Add("Bill");
names.Remove("Ana");
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

Console.WriteLine($"My name is {names[0]}");
Console.WriteLine($"I've added {names[2]} and {names[3]} to the list");

Console.WriteLine($"The list has {names.Count} people in it");

var index = names.IndexOf("Felipe");
Console.WriteLine($"The name {names[index]} is at index {index}");

var notFound = names.IndexOf("Not Found");
Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");

names.Sort();
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}
}
#endregion
Console.WriteLine($"Hello {name.ToUpper()}!");
}
}
3 changes: 2 additions & 1 deletion csharp/list-quickstart/list-quickstart.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<nullable>enable</nullable>
</PropertyGroup>

</Project>
3 changes: 2 additions & 1 deletion csharp/numbers-quickstart/NumbersInCSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<nullable>enable</nullable>
</PropertyGroup>

</Project>
Loading

0 comments on commit a39cf54

Please sign in to comment.