Skip to content
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

Merge main into live #36146

Merged
merged 10 commits into from
Jul 11, 2023
9 changes: 6 additions & 3 deletions docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
"_csharplang/proposals/csharp-9.0/*.md": "07/29/2020",
"_csharplang/proposals/csharp-10.0/*.md": "08/07/2021",
"_csharplang/proposals/csharp-11.0/*.md": "09/30/2022",
"_csharplang/proposals/*.md": "04/03/2023",
"_csharplang/proposals/*.md": "07/06/2023",
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "11/08/2022",
"_vblang/spec/*.md": "07/21/2017"
},
Expand Down Expand Up @@ -665,6 +665,8 @@
"_csharplang/proposals/primary-constructors.md": "Primary constructors",
"_csharplang/proposals/using-alias-types.md": "Alias any type",
"_csharplang/proposals/lambda-method-group-defaults.md": "Optional and parameter array parameters for lambdas and method groups",
"_csharplang/proposals/inline-arrays.md": "Inline arrays, or fixed sized buffers",


"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "C# compiler breaking changes since C# 10",
"_vblang/spec/introduction.md": "Introduction",
Expand Down Expand Up @@ -774,8 +776,9 @@

"_csharplang/proposals/primary-constructors.md": "Primary constructors put the parameters of one constructor in scope for the whole class or struct to be used for initialization or directly as object state. The trade-off is that any other constructors must call through the primary constructor.",
"_csharplang/proposals/using-alias-types.md": "Using directives can alias any type, not just named types. You can create aliases for tuple types, generics and more.",
"_csharplang/proposals/lambda-method-group-defaults.md": "Lambda expressions can now declare default parameters and params arrays parameters.",

"_csharplang/proposals/lambda-method-group-defaults.md": "Optional and parameter array parameters for lambdas and method groups",
"_csharplang/proposals/inline-arrays.md": "Inline arrays provide a general-purpose and safe mechanism for declaring inline arrays within C# classes, structs, and interfaces.",

"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10",
"_vblang/spec/introduction.md": "This chapter provides and introduction to the Visual Basic language.",
"_vblang/spec/lexical-grammar.md": "This chapter defines the lexical grammar for Visual Basic.",
Expand Down
9 changes: 6 additions & 3 deletions docs/core/tools/dotnet-install-script.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: dotnet-install scripts
description: Learn about the dotnet-install scripts to install the .NET SDK and the shared runtime.
ms.date: 11/08/2021
ms.date: 07/10/2023
---
# dotnet-install scripts reference

Expand Down Expand Up @@ -45,8 +45,8 @@ The bash script also reads PowerShell switches, so you can use PowerShell switch

The `dotnet-install` scripts perform a non-admin installation of the .NET SDK, which includes the .NET CLI and the shared runtime. There are two scripts:

* A PowerShell script that works on Windows.
* A bash script that works on Linux/macOS.
* A PowerShell script that works on Windows. For installation instructions, see [Install on Windows](../install/windows.md#install-with-powershell-automation).
* A bash script that works on Linux/macOS. For installation instructions, see [Install on Linux](../install/linux-scripted-manual.md#scripted-install) and [Install on macOS](../install/macos.md#install-with-bash-automation).

> [!NOTE]
> .NET collects telemetry data. To learn more and how to opt out, see [.NET SDK telemetry](telemetry.md).
Expand Down Expand Up @@ -80,6 +80,9 @@ By default, the installation scripts download the SDK and install it. If you wis

By default, the script adds the install location to the $PATH for the current session. Override this default behavior by specifying the `-NoPath|--no-path` argument. The script doesn't set the `DOTNET_ROOT` environment variable.

> [!IMPORTANT]
> The script doesn't add the install location to the user's `PATH` environment variable, you must manually add it.

Before running the script, install the required [dependencies](../install/windows.md#dependencies).

You can install a specific version using the `-Version|--version` argument. The version must be specified as a three-part version number, such as `2.1.0`. If the version isn't specified, the script installs the `latest` version.
Expand Down
8 changes: 4 additions & 4 deletions docs/csharp/fundamentals/tutorials/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ Did you notice that the account number is blank? It's time to fix that. The acco
Add a member declaration to the `BankAccount` class. Place the following line of code after the opening brace `{` at the beginning of the `BankAccount` class:

```csharp
private static int accountNumberSeed = 1234567890;
private static int s_accountNumberSeed = 1234567890;
```

The `accountNumberSeed` is a data member. It's `private`, which means it can only be accessed by code inside the `BankAccount` class. It's a way of separating the public responsibilities (like having an account number) from the private implementation (how account numbers are generated). It's also `static`, which means it's shared by all of the `BankAccount` objects. The value of a non-static variable is unique to each instance of the `BankAccount` object. Add the following two lines to the constructor to assign the account number. Place them after the line that says `this.Balance = initialBalance`:
The `accountNumberSeed` is a data member. It's `private`, which means it can only be accessed by code inside the `BankAccount` class. It's a way of separating the public responsibilities (like having an account number) from the private implementation (how account numbers are generated). It's also `static`, which means it's shared by all of the `BankAccount` objects. The value of a non-static variable is unique to each instance of the `BankAccount` object. The `accountNumberSeed` is a `private static` field and thus has the `s_` prefix as per C# naming conventions. The `s` denoting `static` and `_` denoting `private` field. Add the following two lines to the constructor to assign the account number. Place them after the line that says `this.Balance = initialBalance`:

```csharp
this.Number = accountNumberSeed.ToString();
this.Number = s_accountNumberSeed.ToString();
accountNumberSeed++;
```

Expand All @@ -133,7 +133,7 @@ This example shows an important aspect of ***properties***. You're now computing

Next, implement the `MakeDeposit` and `MakeWithdrawal` methods. These methods will enforce the final two rules: the initial balance must be positive, and any withdrawal must not create a negative balance.

These rules introduce the concept of ***exceptions***. The standard way of indicating that a method can't complete its work successfully is to throw an exception. The type of exception and the message associated with it describe the error. Here, the `MakeDeposit` method throws an exception if the amount of the deposit isn't greater than 0. The `MakeWithdrawal` method throws an exception if the withdrawal amount isn't greater than 0, or if applying the withdrawal results in a negative balance. Add the following code after the declaration of the `allTransactions` list:
These rules introduce the concept of ***exceptions***. The standard way of indicating that a method can't complete its work successfully is to throw an exception. The type of exception and the message associated with it describe the error. Here, the `MakeDeposit` method throws an exception if the amount of the deposit isn't greater than 0. The `MakeWithdrawal` method throws an exception if the withdrawal amount isn't greater than 0, or if applying the withdrawal results in a negative balance. Add the following code after the declaration of the `_allTransactions` list:

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

Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/fundamentals/tutorials/oop.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ This implementation calls `MakeDeposit` only if the initial balance is greater t
Now that the `BankAccount` class has a read-only field for the minimum balance, the final change is to change the hard code `0` to `minimumBalance` in the `MakeWithdrawal` method:

```csharp
if (Balance - amount < minimumBalance)
if (Balance - amount < _minimumBalance)
```

After extending the `BankAccount` class, you can modify the `LineOfCreditAccount` constructor to call the new base constructor, as shown in the following code:
Expand All @@ -157,7 +157,7 @@ public void MakeWithdrawal(decimal amount, DateTime date, string note)
throw new InvalidOperationException("Not sufficient funds for this withdrawal");
}
var withdrawal = new Transaction(-amount, date, note);
allTransactions.Add(withdrawal);
_allTransactions.Add(withdrawal);
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public decimal Balance
get
{
decimal balance = 0;
foreach (var item in allTransactions)
foreach (var item in _allTransactions)
{
balance += item.Amount;
}
Expand All @@ -23,20 +23,20 @@ public decimal Balance
}
#endregion

private static int accountNumberSeed = 1234567890;
private static int s_accountNumberSeed = 1234567890;
#region Constructor
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;
Number = s_accountNumberSeed.ToString();
s_accountNumberSeed++;

Owner = name;
MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}
#endregion

#region TransactionDeclaration
private List<Transaction> allTransactions = new List<Transaction>();
private List<Transaction> _allTransactions = new List<Transaction>();
#endregion

#region DepositAndWithdrawal
Expand All @@ -47,7 +47,7 @@ public void MakeDeposit(decimal amount, DateTime date, string note)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
}
var deposit = new Transaction(amount, date, note);
allTransactions.Add(deposit);
_allTransactions.Add(deposit);
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
Expand All @@ -61,7 +61,7 @@ public void MakeWithdrawal(decimal amount, DateTime date, string note)
throw new InvalidOperationException("Not sufficient funds for this withdrawal");
}
var withdrawal = new Transaction(-amount, date, note);
allTransactions.Add(withdrawal);
_allTransactions.Add(withdrawal);
}
#endregion

Expand All @@ -72,7 +72,7 @@ public string GetAccountHistory()

decimal balance = 0;
report.AppendLine("Date\t\tAmount\tBalance\tNote");
foreach (var item in allTransactions)
foreach (var item in _allTransactions)
{
balance += item.Amount;
report.AppendLine($"{item.Date.ToShortDateString()}\t{item.Amount}\t{balance}\t{item.Notes}");
Expand Down
3 changes: 3 additions & 0 deletions docs/csharp/fundamentals/types/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ The following code example shows how client code uses the generic `GenericList<T

:::code language="csharp" source="snippets/generics/Program.cs" ID="Snippet3":::

> [!NOTE]
> Following examples applies not only to `class` types, but also `interface` and `struct` types

## Generics overview

- Use generic types to maximize code reuse, type safety, and performance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,15 @@ public static void Main()
// </WithExpression>
}
}

namespace InlineArrays
{
// <DeclareInlineArray>
[System.Runtime.CompilerServices.InlineArray(10)]
public struct CharBuffer
{
private char _firstElement;
}
// </DeclareInlineArray>
}
}
26 changes: 24 additions & 2 deletions docs/csharp/language-reference/builtin-types/struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The following code defines a `readonly` struct with init-only property setters,

You can also use the `readonly` modifier to declare that an instance member doesn't modify the state of a struct. If you can't declare the whole structure type as `readonly`, use the `readonly` modifier to mark the instance members that don't modify the state of the struct.

Within a `readonly` instance member, you can't assign to structure's instance fields. However, a `readonly` member can call a non-`readonly` member. In that case the compiler creates a copy of the structure instance and calls the non-`readonly` member on that copy. As a result, the original structure instance isn't modified.
Within a `readonly` instance member, you can't assign to structure's instance fields. However, a `readonly` member can call a non-`readonly` member. In that case, the compiler creates a copy of the structure instance and calls the non-`readonly` member on that copy. As a result, the original structure instance isn't modified.

Typically, you apply the `readonly` modifier to the following kinds of instance members:

Expand Down Expand Up @@ -82,6 +82,28 @@ Beginning with C# 10, you can use the [`with` expression](../operators/with-expr

Beginning with C# 10, you can define record structure types. Record types provide built-in functionality for encapsulating data. You can define both `record struct` and `readonly record struct` types. A record struct can't be a [`ref struct`](ref-struct.md). For more information and examples, see [Records](record.md).

## Inline arrays

Beginning with C# 12, you can declare *inline arrays* as a `struct` type:

:::code language="csharp" source="snippets/shared/StructType.cs" id="DeclareInlineArray":::

An inline array is a structure that contains a contiguous block of N elements of the same type. It's a safe-code equivalent of the [fixed buffer](../unsafe-code.md#fixed-size-buffers) declaration available only in unsafe code. An inline array is a `struct` with the following characteristics:

- It contains a single field.
- The struct doesn't specify an explicit layout.

In addition, the compiler validates the <xref:System.Runtime.CompilerServices.InlineArrayAttribute?displayProperty=fullName> attribute:

- The length must be greater than zero (`> 0`).
- The target type must be a struct.

In most cases, an inline array can be accessed like an array, both to read and write values. In addition, you can use the [range](../operators/member-access-operators.md#range-operator-) and [index](../operators/member-access-operators.md#indexer-access) operators.

There are minimal restrictions on the type of the single field. It can't be a pointer type, but it can be any reference type, or any value type. You can use inline arrays with almost any C# data structure.

Inline arrays are an advanced language feature. They're intended for high-performance scenarios where an inline, contiguous block of elements is faster than other alternative data structures. You can learn more about inline arrays from the [feature speclet](~/_csharplang/proposals/inline-arrays.md)

## Struct initialization and default values

A variable of a `struct` type directly contains the data for that `struct`. That creates a distinction between an uninitialized `struct`, which has its default value and an initialized `struct`, which stores values set by constructing it. For example consider the following code:
Expand All @@ -108,7 +130,7 @@ Beginning with C# 11, if you don't initialize all fields in a struct, the compil

Every `struct` has a `public` parameterless constructor. If you write a parameterless constructor, it must be public. If a struct declares any field initializers, it must explicitly declare a constructor. That constructor need not be parameterless. If a struct declares a field initializer but no constructors, the compiler reports an error. Any explicitly declared constructor (with parameters, or parameterless) executes all field initializers for that struct. All fields without a field initializer or an assignment in a constructor are set to the [default value](default-values.md). For more information, see the [Parameterless struct constructors](~/_csharplang/proposals/csharp-10.0/parameterless-struct-constructors.md) feature proposal note.

Beginning with C# 12, `struct` types can define a [primary constructor](../../programming-guide/classes-and-structs/instance-constructors.md#primary-constructors) as part of its declaration. This provides a concise syntax for constructor parameters that can be used throughout the `struct` body, in any member declaration for that struct.
Beginning with C# 12, `struct` types can define a [primary constructor](../../programming-guide/classes-and-structs/instance-constructors.md#primary-constructors) as part of its declaration. Primary constructors provides a concise syntax for constructor parameters that can be used throughout the `struct` body, in any member declaration for that struct.

If all instance fields of a structure type are accessible, you can also instantiate it without the `new` operator. In that case you must initialize all instance fields before the first use of the instance. The following example shows how to do that:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The element value is the warning level you want displayed for the compilation: L
| 1 | Displays severe warning messages. |
| 2 | Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members. |
| 3 | Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to `true` or `false`. |
| 4 (the default)|Displays all level 3 warnings plus informational warnings. |
| 4 (default) | Displays all level 3 warnings plus informational warnings. |

> [!WARNING]
> The compiler command line accepts values greater than 4 to enable [warning wave warnings](../compiler-messages/warning-waves.md). However, the .NET SDK sets the *WarningLevel* to match the *AnalysisLevel* in your project file.
Expand All @@ -53,6 +53,12 @@ To get information about an error or warning, you can look up the error code in

## Analysis level

The **AnalysisLevel** option specifies additional [warning waves](../compiler-messages/warning-waves.md) and analyzers to enable. Warning wave warnings are additional checks that improve your code, or ensure it will be compatible with upcoming releases. Analyzers provide lint-like capability to improve your code.

```xml
<AnalysisLevel>preview</AnalysisLevel>
```

| Analysis level | Meaning |
|------------------|---------|
| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023---a-static-type-is-used-in-an-is-or-as-expression). |
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The preceding example allows a caller to create a `Person` using the default con

:::code language="csharp" source="./snippets/properties/Person.cs" id="Snippet9.2":::

The preceding code makes two addition to the `Person` class. First, the `FirstName` property declaration includes the `required` modifier. That means any code that creates a new `Person` must set this property. Second, the constructor that takes a `firstName` parameter has the <xref:System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute?displayProperty=fullName> attribute. This attribute informs the compiler that this constructor sets *all* `required` members.
The preceding code makes two additions to the `Person` class. First, the `FirstName` property declaration includes the `required` modifier. That means any code that creates a new `Person` must set this property. Second, the constructor that takes a `firstName` parameter has the <xref:System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute?displayProperty=fullName> attribute. This attribute informs the compiler that this constructor sets *all* `required` members.

> [!IMPORTANT]
> Don't confuse `required` with *non-nullable*. It's valid to set a `required` property to `null` or `default`. If the type is non-nullable, such as `string` in these examples, the compiler issues a warning.
Expand Down
2 changes: 2 additions & 0 deletions docs/csharp/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1426,3 +1426,5 @@ items:
href: ../../_csharplang/proposals/using-alias-types.md
- name: Optional Lambda expression parameters
href: ../../_csharplang/proposals/lambda-method-group-defaults.md
- name: Inline arrays
href: ../../_csharplang/proposals/inline-arrays.md
Loading