Skip to content

Commit 0e4df77

Browse files
Update tutorials to .NET 6 (#28404)
* Update to .NET 6 Use the latest .NET 6 template. Fixes #27896 * Update tutorials to .NET 6 Fixes #27896 Also, in this PR, update all the tutorials in the Get Started and Tutorials section to .NET 6. That includes using top-level statements, implicit usings, and following other conventions. Because I changed so much of the included code, I updated the date on the associated articles even though the text didn't change as much. * Apply suggestions from code review Co-authored-by: David Pine <[email protected]> * Apply suggestions from code review * Update docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/Program.cs Co-authored-by: David Pine <[email protected]>
1 parent f78bd66 commit 0e4df77

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1068
-1191
lines changed

docs/csharp/fundamentals/tutorials/classes.md

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Classes and objects - C# Fundamentals tutorial
33
description: Create your first C# program and explore object oriented concepts
4-
ms.date: 05/14/2021
4+
ms.date: 02/25/2022
55
---
66
# Explore object oriented programming with classes and objects
77

@@ -16,21 +16,11 @@ The tutorial expects that you have a machine set up for local development. On Wi
1616
Using a terminal window, create a directory named *classes*. You'll build your application there. Change to that directory and type `dotnet new console` in the console window. This command creates your application. Open *Program.cs*. It should look like this:
1717

1818
```csharp
19-
using System;
20-
21-
namespace classes
22-
{
23-
class Program
24-
{
25-
static void Main(string[] args)
26-
{
27-
Console.WriteLine("Hello World!");
28-
}
29-
}
30-
}
19+
// See https://aka.ms/new-console-template for more information
20+
Console.WriteLine("Hello, World!");
3121
```
3222

33-
In this tutorial, you're going to create new types that represent a bank account. Typically developers define each class in a different text file. That makes it easier to manage as a program grows in size. Create a new file named *BankAccount.cs* in the *classes* directory.
23+
In this tutorial, you're going to create new types that represent a bank account. Typically developers define each class in a different text file. That makes it easier to manage as a program grows in size. Create a new file named *BankAccount.cs* in the *Classes* directory.
3424

3525
This file will contain the definition of a ***bank account***. Object Oriented programming organizes code by creating types in the form of ***classes***. These classes contain the code that represents a specific entity. The `BankAccount` class represents a bank account. The code implements specific operations through methods and properties. In this tutorial, the bank account supports this behavior:
3626

@@ -47,23 +37,20 @@ This file will contain the definition of a ***bank account***. Object Oriented p
4737
You can start by creating the basics of a class that defines that behavior. Create a new file using the **File:New** command. Name it *BankAccount.cs*. Add the following code to your *BankAccount.cs* file:
4838

4939
```csharp
50-
using System;
40+
namespace Classes;
5141

52-
namespace classes
42+
public class BankAccount
5343
{
54-
public class BankAccount
55-
{
56-
public string Number { get; }
57-
public string Owner { get; set; }
58-
public decimal Balance { get; }
44+
public string Number { get; }
45+
public string Owner { get; set; }
46+
public decimal Balance { get; }
5947

60-
public void MakeDeposit(decimal amount, DateTime date, string note)
61-
{
62-
}
48+
public void MakeDeposit(decimal amount, DateTime date, string note)
49+
{
50+
}
6351

64-
public void MakeWithdrawal(decimal amount, DateTime date, string note)
65-
{
66-
}
52+
public void MakeWithdrawal(decimal amount, DateTime date, string note)
53+
{
6754
}
6855
}
6956
```
@@ -89,6 +76,8 @@ public BankAccount(string name, decimal initialBalance)
8976
Constructors are called when you create an object using [`new`](../../language-reference/operators/new-operator.md). Replace the line `Console.WriteLine("Hello World!");` in *Program.cs* with the following code (replace `<name>` with your name):
9077

9178
```csharp
79+
using Classes;
80+
9281
var account = new BankAccount("<name>", 1000);
9382
Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance} initial balance.");
9483
```
@@ -124,12 +113,6 @@ Now, let's add a <xref:System.Collections.Generic.List%601> of `Transaction` obj
124113

125114
:::code language="csharp" source="./snippets/introduction-to-classes/BankAccount.cs" id="TransactionDeclaration":::
126115

127-
The <xref:System.Collections.Generic.List%601> class requires you to import a different namespace. Add the following at the beginning of *BankAccount.cs*:
128-
129-
```csharp
130-
using System.Collections.Generic;
131-
```
132-
133116
Now, let's correctly compute the `Balance`. The current balance can be found by summing the values of all transactions. As the code is currently, you can only get the initial balance of the account, so you'll have to update the `Balance` property. Replace the line `public decimal Balance { get; }` in *BankAccount.cs* with the following code:
134117

135118
:::code language="csharp" source="./snippets/introduction-to-classes/BankAccount.cs" id="BalanceComputation":::

docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "How to display command-line arguments"
33
description: Learn how to display command-line arguments. See a code example and view additional available resources.
4-
ms.date: 03/08/2021
4+
ms.date: 02/25/2022
55
ms.topic: how-to
66
helpviewer_keywords:
77
- "command-line arguments [C#], displaying"

docs/csharp/fundamentals/tutorials/inheritance.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Inheritance in C#
33
description: Learn to use inheritance in C# libraries and applications.
4-
ms.date: 07/05/2018
4+
ms.date: 02/25/2022
55
ms.assetid: aeb68c74-0ea0-406f-9fbe-2ce02d47ef31
66
---
77
# Inheritance in C# and .NET
@@ -93,8 +93,6 @@ public class B : A // Generates CS0534.
9393
Inheritance applies only to classes and interfaces. Other type categories (structs, delegates, and enums) do not support inheritance. Because of these rules, attempting to compile code like the following example produces compiler error CS0527: "Type 'ValueType' in interface list is not an interface." The error message indicates that, although you can define the interfaces that a struct implements, inheritance is not supported.
9494

9595
```csharp
96-
using System;
97-
9896
public struct ValueStructure : ValueType // Generates CS0527.
9997
{
10098
}

docs/csharp/fundamentals/tutorials/oop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Object-Oriented Programming (C#)"
33
description: C# provides full support for object-oriented programming including abstraction, encapsulation, inheritance, and polymorphism.
4-
ms.date: 05/14/2021
4+
ms.date: 02/25/2022
55
---
66
# Object-Oriented programming (C#)
77

docs/csharp/fundamentals/tutorials/pattern-matching.md

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Tutorial: Build algorithms with pattern matching"
33
description: This advanced tutorial demonstrates how to use pattern matching techniques to create functionality using data and algorithms that are created separately.
4-
ms.date: 10/01/2021
4+
ms.date: 02/25/2022
55
ms.custom: contperf-fy21q1
66
---
77
# Tutorial: Use pattern matching to build type-driven and data-driven algorithms
@@ -64,21 +64,20 @@ using CommercialRegistration;
6464
using ConsumerVehicleRegistration;
6565
using LiveryRegistration;
6666

67-
namespace toll_calculator
67+
namespace Calculators;
68+
69+
public class TollCalculator
6870
{
69-
public class TollCalculator
71+
public decimal CalculateToll(object vehicle) =>
72+
vehicle switch
7073
{
71-
public decimal CalculateToll(object vehicle) =>
72-
vehicle switch
73-
{
74-
Car c => 2.00m,
75-
Taxi t => 3.50m,
76-
Bus b => 5.00m,
77-
DeliveryTruck t => 10.00m,
78-
{ } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
79-
null => throw new ArgumentNullException(nameof(vehicle))
80-
};
81-
}
74+
Car c => 2.00m,
75+
Taxi t => 3.50m,
76+
Bus b => 5.00m,
77+
DeliveryTruck t => 10.00m,
78+
{ } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
79+
null => throw new ArgumentNullException(nameof(vehicle))
80+
};
8281
}
8382
```
8483

@@ -92,42 +91,35 @@ using CommercialRegistration;
9291
using ConsumerVehicleRegistration;
9392
using LiveryRegistration;
9493

95-
namespace toll_calculator
94+
using toll_calculator;
95+
96+
var tollCalc = new TollCalculator();
97+
98+
var car = new Car();
99+
var taxi = new Taxi();
100+
var bus = new Bus();
101+
var truck = new DeliveryTruck();
102+
103+
Console.WriteLine($"The toll for a car is {tollCalc.CalculateToll(car)}");
104+
Console.WriteLine($"The toll for a taxi is {tollCalc.CalculateToll(taxi)}");
105+
Console.WriteLine($"The toll for a bus is {tollCalc.CalculateToll(bus)}");
106+
Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(truck)}");
107+
108+
try
96109
{
97-
class Program
98-
{
99-
static void Main(string[] args)
100-
{
101-
var tollCalc = new TollCalculator();
102-
103-
var car = new Car();
104-
var taxi = new Taxi();
105-
var bus = new Bus();
106-
var truck = new DeliveryTruck();
107-
108-
Console.WriteLine($"The toll for a car is {tollCalc.CalculateToll(car)}");
109-
Console.WriteLine($"The toll for a taxi is {tollCalc.CalculateToll(taxi)}");
110-
Console.WriteLine($"The toll for a bus is {tollCalc.CalculateToll(bus)}");
111-
Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(truck)}");
112-
113-
try
114-
{
115-
tollCalc.CalculateToll("this will fail");
116-
}
117-
catch (ArgumentException e)
118-
{
119-
Console.WriteLine("Caught an argument exception when using the wrong type");
120-
}
121-
try
122-
{
123-
tollCalc.CalculateToll(null!);
124-
}
125-
catch (ArgumentNullException e)
126-
{
127-
Console.WriteLine("Caught an argument exception when using null");
128-
}
129-
}
130-
}
110+
tollCalc.CalculateToll("this will fail");
111+
}
112+
catch (ArgumentException e)
113+
{
114+
Console.WriteLine("Caught an argument exception when using the wrong type");
115+
}
116+
try
117+
{
118+
tollCalc.CalculateToll(null!);
119+
}
120+
catch (ArgumentNullException e)
121+
{
122+
Console.WriteLine("Caught an argument exception when using null");
131123
}
132124
```
133125

docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "How to safely cast by using pattern matching and the is and as operators"
33
description: Learn to use pattern matching techniques to safely cast variables to a different type. You can use pattern matching as well as the is and as operators to safely convert types.
4-
ms.date: 05/14/2021
4+
ms.date: 02/25/2022
55
helpviewer_keywords:
66
- "cast operators [C#], as and is operators"
77
- "as operator [C#]"

docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/Program.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
using System;
1+
// The Length property provides the number of array elements.
2+
Console.WriteLine($"parameter count = {args.Length}");
23

3-
class CommandLine
4+
for (int i = 0; i < args.Length; i++)
45
{
5-
static void Main(string[] args)
6-
{
7-
// The Length property provides the number of array elements.
8-
Console.WriteLine($"parameter count = {args.Length}");
9-
10-
for (int i = 0; i < args.Length; i++)
11-
{
12-
Console.WriteLine($"Arg[{i}] = [{args[i]}]");
13-
}
14-
}
6+
Console.WriteLine($"Arg[{i}] = [{args[i]}]");
157
}
8+
169
/* Output (assumes 3 cmd line args):
1710
parameter count = 3
1811
Arg[0] = [a]

docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/command-line-arguments.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
<RootNamespace>command_line_arguments</RootNamespace>
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
79
</PropertyGroup>
810

911
</Project>
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
using System;
1+
namespace Inheritance;
22

3-
namespace inheritance
3+
class Program
44
{
5-
class Program
5+
static void Main(string[] args)
66
{
7-
static void Main(string[] args)
8-
{
9-
Basic.Example.Main();
10-
IsA_Namespace.Example.Main();
11-
AccessExample.Main(args);
12-
Example.Example.Main();
13-
ClassNameExample.Main();
14-
ClassExample.Main();
15-
}
7+
Basic.Example.Main();
8+
IsA_Namespace.Example.Main();
9+
AccessExample.Main(args);
10+
Example.Example.Main();
11+
ClassNameExample.Main();
12+
ClassExample.Main();
1613
}
1714
}

0 commit comments

Comments
 (0)