Skip to content

Commit

Permalink
Fix up using directive vs. using statement (#41332)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren authored Jun 7, 2024
1 parent bf6a764 commit b55fd6d
Show file tree
Hide file tree
Showing 80 changed files with 1,239 additions and 1,265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public partial class Counter : System.Web.UI.UserControl
}
```

In Blazor, you can register handlers for DOM UI events directly using directive attributes of the form `@on{event}`. The `{event}` placeholder represents the name of the event. For example, you can listen for button clicks like this:
In Blazor, you can register handlers for DOM UI events directly `using` directive attributes of the form `@on{event}`. The `{event}` placeholder represents the name of the event. For example, you can listen for button clicks like this:

```razor
<button @onclick="OnClick">Click me!</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ builder.Services.AddDefaultIdentity<IdentityUser>(options =>
.AddEntityFrameworkStores<ApplicationDbContext>();
```

The `AddDefaultIdentity` extension method is used to configure Identity to use the default `ApplicationDbContext` and the framework's `IdentityUser` type. If you're using a custom `IdentityUser`, be sure to specify its type here. If these extension methods aren't working in your application, check that you have the appropriate using statements and that you have the necessary NuGet package references. For example, your project should have some version of the `Microsoft.AspNetCore.Identity.EntityFrameworkCore` and `Microsoft.AspNetCore.Identity.UI` packages referenced.
The `AddDefaultIdentity` extension method is used to configure Identity to use the default `ApplicationDbContext` and the framework's `IdentityUser` type. If you're using a custom `IdentityUser`, be sure to specify its type here. If these extension methods aren't working in your application, check that you have the appropriate `using` directives and that you have the necessary NuGet package references. For example, your project should have some version of the `Microsoft.AspNetCore.Identity.EntityFrameworkCore` and `Microsoft.AspNetCore.Identity.UI` packages referenced.

Also in *Program.cs* you should see the necessary middleware configured for the site. Specifically, `UseAuthentication` and `UseAuthorization` should be set up, and in the proper location.

Expand Down
6 changes: 3 additions & 3 deletions docs/azure/sdk/includes/implement-defaultazurecredential.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Right click on your project node in Visual Studio and select **Manage NuGet Pack

Azure services are generally accessed using corresponding client classes from the SDK. These classes and your own custom services should be registered in the `Program.cs` file so they can be accessed via dependency injection throughout your app. Inside of `Program.cs`, follow the steps below to correctly setup your service and `DefaultAzureCredential`.

1. Include the `Azure.Identity` and `Microsoft.Extensions.Azure` namespaces with a using statement.
1. Include the `Azure.Identity` and `Microsoft.Extensions.Azure` namespaces with a `using` directive.
1. Register the Azure service using relevant helper methods.
1. Pass an instance of the `DefaultAzureCredential` object to the `UseCredential` method.

Expand All @@ -47,12 +47,12 @@ Alternatively, you can also utilize `DefaultAzureCredential` in your services mo
using Azure.Identity;

// Inside of Program.cs
builder.Services.AddSingleton<BlobServiceClient>(x =>
builder.Services.AddSingleton<BlobServiceClient>(x =>
new BlobServiceClient(
new Uri("https://<account-name>.blob.core.windows.net"),
new DefaultAzureCredential()));
```

When the above code is run on your local workstation during local development, it will look in the environment variables for an application service principal or at Visual Studio, VS Code, the Azure CLI, or Azure PowerShell for a set of developer credentials, either of which can be used to authenticate the app to Azure resources during local development.
When the above code is run on your local workstation during local development, it will look in the environment variables for an application service principal or at Visual Studio, VS Code, the Azure CLI, or Azure PowerShell for a set of developer credentials, either of which can be used to authenticate the app to Azure resources during local development.

When deployed to Azure this same code can also authenticate your app to other Azure resources. `DefaultAzureCredential` can retrieve environment settings and managed identity configurations to authenticate to other services automatically.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Default projects should compile.

## Recommended action

If you relied on the implicit global using directive, you can:
If you relied on the implicit global `using` directive, you can:

- Add a [global using directive](../../../../csharp/language-reference/keywords/using-directive.md#global-modifier) to one of your source files.
- Add a using directive to each source code file that uses APIs from System.Net.Http.
- Add a [global `using` directive](../../../../csharp/language-reference/keywords/using-directive.md#global-modifier) to one of your source files.
- Add a `using` directive to each source code file that uses APIs from System.Net.Http.

## Affected APIs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ Applications and libraries add distributed tracing instrumentation using the
### ActivitySource

First create an instance of ActivitySource. ActivitySource provides APIs to create and
start Activity objects. Add the static ActivitySource variable above Main() and
`using System.Diagnostics;` to the using statements.
start Activity objects. Add the static ActivitySource variable above `Main()` and
`using System.Diagnostics;` to the `using` directives.

```csharp
using OpenTelemetry;
Expand Down
4 changes: 2 additions & 2 deletions docs/core/extensions/dependency-injection-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ Update *Program.cs* with the following code:

Each `services.Add{LIFETIME}<{SERVICE}>` extension method adds (and potentially configures) services. We recommend that apps follow this convention. Don't place extension methods in the <xref:Microsoft.Extensions.DependencyInjection?displayProperty=fullName> namespace unless you're authoring an official Microsoft package. Extension methods that are defined within the `Microsoft.Extensions.DependencyInjection` namespace:

- Are displayed in [IntelliSense](/visualstudio/ide/using-intellisense) without requiring additional `using` blocks.
- Reduce the number of required `using` statements in the `Program` or `Startup` classes where these extension methods are typically called.
- Are displayed in [IntelliSense](/visualstudio/ide/using-intellisense) without requiring additional `using` directives.
- Reduce the number of required `using` directives in the `Program` or `Startup` classes where these extension methods are typically called.

The app:

Expand Down
4 changes: 2 additions & 2 deletions docs/core/extensions/file-globbing.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The preceding C# code:
- Calls `Execute` given the `DirectoryInfoWrapper` instance to yield a <xref:Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult> object.

> [!NOTE]
> The `DirectoryInfoWrapper` type is defined in the `Microsoft.Extensions.FileSystemGlobbing.Abstractions` namespace, and the `DirectoryInfo` type is defined in the `System.IO` namespace. To avoid unnecessary `using` statements, you can use the provided extension methods.
> The `DirectoryInfoWrapper` type is defined in the `Microsoft.Extensions.FileSystemGlobbing.Abstractions` namespace, and the `DirectoryInfo` type is defined in the `System.IO` namespace. To avoid unnecessary `using` directives, you can use the provided extension methods.
There is another extension method that yields an `IEnumerable<string>` representing the matching files:

Expand Down Expand Up @@ -146,7 +146,7 @@ The additional `Match` overloads work in similar ways.
The patterns that are specified in the `AddExclude` and `AddInclude` methods can use the following formats to match multiple files or directories.

- Exact directory or file name

- `some-file.txt`
- `path/to/file.txt`

Expand Down
10 changes: 5 additions & 5 deletions docs/core/extensions/windows-service-with-installer.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dotnet add App.WindowsService.csproj package CliWrap

For more information, see [dotnet add package](../tools/dotnet-add-package.md).

With `CliWrap` installed, open the _Program.cs_ file of the `App.WindowsService` project. After the `using` statements, but before the `IHost` is created, add the following code:
With `CliWrap` installed, open the _Program.cs_ file of the `App.WindowsService` project. After the `using` directives, but before the `IHost` is created, add the following code:

```csharp
using CliWrap;
Expand All @@ -86,7 +86,7 @@ if (args is { Length: 1 })
{
string executablePath =
Path.Combine(AppContext.BaseDirectory, "App.WindowsService.exe");

if (args[0] is "/Install")
{
await Cli.Wrap("sc")
Expand All @@ -98,7 +98,7 @@ if (args is { Length: 1 })
await Cli.Wrap("sc")
.WithArguments(new[] { "stop", ServiceName })
.ExecuteAsync();

await Cli.Wrap("sc")
.WithArguments(new[] { "delete", ServiceName })
.ExecuteAsync();
Expand Down Expand Up @@ -181,7 +181,7 @@ After the project reference has been added, configure the _Package.wxs_ file. Op
Version="$(Version)"
UpgradeCode="$(var.UpgradeCode)"
Compressed="true">

<!-- Allow upgrades and prevent downgrades -->
<MajorUpgrade DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit." />

Expand All @@ -204,7 +204,7 @@ After the project reference has been added, configure the _Package.wxs_ file. Op

<!-- Create a single component which is the App.WindowsService.exe file -->
<Component Id="ServiceExecutable" Bitness="always64">

<!-- Copies the App.WindowsService.exe file using the
project reference preprocessor variables -->
<File Id="App.WindowsService.exe"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ This tutorial requires the following:

Create a new .NET Core console application. You can create one by using the [`dotnet new console`](../../core/tools/dotnet-new-sdk-templates.md#console) command or from [Visual Studio](/visualstudio/install/install-visual-studio). Open the *Program.cs* file in your favorite code editor.

### Replace using statements
### Replace using directives

Replace the existing using statements with these declarations:
Replace the existing `using` directives with these declarations:

```csharp
using System;
Expand Down Expand Up @@ -100,7 +100,7 @@ static async Task Main()
Console.WriteLine("\nENTER key pressed: cancelling downloads.\n");
s_cts.Cancel();
});

Task sumPageSizesTask = SumPageSizesAsync();

Task finishedTask = await Task.WhenAny(new[] { cancelTask, sumPageSizesTask });
Expand All @@ -117,7 +117,7 @@ static async Task Main()
Console.WriteLine("Download task has been cancelled.");
}
}

Console.WriteLine("Application ending.");
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ In a project with top-level statements, you can't use the [-main](../../language

## `using` directives

If you include using directives, they must come first in the file, as in this example:
If you include `using` directives, they must come first in the file, as in this example:

:::code language="csharp" source="snippets/top-level-statements-1/Program.cs":::

Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/language-reference/compiler-messages/cs0103.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ms.assetid: fd1f2104-a945-4dba-8137-8ef869826062

The name 'identifier' does not exist in the current context

An attempt was made to use a name that does not exist in the class, [namespace](../keywords/namespace.md), or scope. Check the spelling of the name and check your using directives and assembly references to make sure that the name that you are trying to use is available.
An attempt was made to use a name that does not exist in the class, [namespace](../keywords/namespace.md), or scope. Check the spelling of the name and check your `using` directives and assembly references to make sure that the name that you are trying to use is available.

This error frequently occurs if you declare a variable in a loop or a `try` or `if` block and then attempt to access it from an enclosing code block or a separate code block, as shown in the following example:

Expand All @@ -32,7 +32,7 @@ class MyClass1
var conn = new MyClass1();
}
catch (Exception e)
{
{
// The following expression causes error CS0103, because variable
// conn only exists in the try block.
if (conn != null)
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/compiler-messages/cs8410.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ author: Youssef1313
---
# Compiler Error CS8410

'type': type used in an asynchronous using statement must be implicitly convertible to 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method.
'type': type used in an asynchronous `using` statement must be implicitly convertible to 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method.

The expression inside an `await using` statement must have a `DisposeAsync` method.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS9096**](#ref-safety-violations): *Cannot ref-assign because source has a wider value escape scope than destination allowing assignment through source of values with narrower escapes scopes than destination.*
- [**CS9101**](#unscoped-ref-restrictions): *UnscopedRefAttribute can only be applied to struct instance methods and properties, and cannot be applied to constructors or init-only members.*
- [**CS9102**](#unscoped-ref-restrictions): *UnscopedRefAttribute cannot be applied to an interface implementation.*
- [**CS9104**](#reference-variable-restrictions): *A using statement resource of type cannot be used in async methods or async lambda expressions.*
- [**CS9104**](#reference-variable-restrictions): *A `using` statement resource of type cannot be used in async methods or async lambda expressions.*
- [**CS9190**](#incorrect-syntax): *`readonly` modifier must be specified after `ref`.*
- [**CS9199**](#reference-variable-restrictions): *A `ref readonly` parameter cannot have the Out attribute.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ f1_keywords:
- "CS9133"
- "CS9162"
- "CS9163"
helpviewer_keywords:
helpviewer_keywords:
- "CS0105"
- "CS0138"
- "CS0431"
Expand Down Expand Up @@ -154,7 +154,7 @@ In addition, if you define an alias named `global`, the compiler issues **CS0440

## Alias name conflicts

You can declare an [alias](../keywords/using-directive.md#using-alias) to a namespace or a type with a using directive:
You can declare an [alias](../keywords/using-directive.md#using-alias) to a namespace or a type with a `using` directive:

:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingAlias":::

Expand Down Expand Up @@ -182,7 +182,7 @@ Beginning with C# 12, these restrictions are introduced:
using InInt = in int;
```

- An `unsafe` using directive must specify an alias, or a `static using`:
- An `unsafe using` directive must specify an alias, or a `static using`:

```csharp
// Elsewhere:
Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/language-reference/keywords/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: "async keyword"
ms.date: 05/22/2017
f1_keywords:
- "async_CSharpKeyword"
helpviewer_keywords:
helpviewer_keywords:
- "async keyword [C#]"
- "async method [C#]"
- "async [C#]"
Expand Down Expand Up @@ -56,7 +56,7 @@ To run the code as a WPF app:
To run the code as a Windows Store app:

- Paste this code into the `MainPage` class in MainPage.xaml.cs.
- Add using directives for System.Net.Http and System.Threading.Tasks.
- Add `using` directives for System.Net.Http and System.Threading.Tasks.

[!code-csharp[wpf-async](../../../../samples/snippets/csharp/language-reference/keywords/async/wpf/MainWindow.xaml.cs#1)]

Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/keywords/using-directive.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using Project = PC.MyCompany.Project;
You can use the `global` modifier on a *using alias directive*.

> [!NOTE]
> The `using` keyword is also used to create *using statements*, which help ensure that <xref:System.IDisposable> objects such as files and fonts are handled correctly. For more information about the *using statement*, see [using statement](../statements/using.md).
> The `using` keyword is also used to create *`using` statements*, which help ensure that <xref:System.IDisposable> objects such as files and fonts are handled correctly. For more information about the *`using` statement*, see [`using` statement](../statements/using.md).
The scope of a `using` directive without the `global` modifier is the file in which it appears.

Expand Down
8 changes: 4 additions & 4 deletions docs/csharp/language-reference/keywords/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
title: "using keyword"
description: "using keyword - C# reference"
ms.date: 04/05/2019
f1_keywords:
f1_keywords:
- "using"
helpviewer_keywords:
helpviewer_keywords:
- "using keyword [C#]"
ms.assetid: 124e1a63-2a4b-4132-b269-3b6d8d3ef72d
---
# using (C# reference)

The `using` keyword has two major uses:

- The [using statement](../statements/using.md) defines a scope at the end of which an object is disposed:
- The [`using` statement](../statements/using.md) defines a scope at the end of which an object is disposed:

:::code language="csharp" source="./snippets/UsingKeywordExample.cs" id="UsingStatement":::

- The [using directive](using-directive.md) creates an alias for a namespace or imports types defined in other namespaces:
- The [`using` directive](using-directive.md) creates an alias for a namespace or imports types defined in other namespaces:

:::code language="csharp" source="./snippets/UsingKeywordExample.cs" id="UsingDirective":::

Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/language-reference/operators/new-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: "The C# new operator is used to create a optionally initialize a ne
ms.date: 11/28/2022
f1_keywords:
- new_CSharpKeyword
helpviewer_keywords:
helpviewer_keywords:
- "new operator keyword [C#]"
ms.assetid: a212b697-a79b-4105-9923-1f7b108036e8
---
Expand Down Expand Up @@ -54,7 +54,7 @@ To create an instance of an [anonymous type](../../fundamentals/types/anonymous-

You don't have to destroy earlier created type instances. Instances of both reference and value types are destroyed automatically. Instances of value types are destroyed as soon as the context that contains them is destroyed. Instances of reference types are destroyed by the [garbage collector](../../../standard/garbage-collection/index.md) at some unspecified time after the last reference to them is removed.

For type instances that contain unmanaged resources, for example, a file handle, it's recommended to employ deterministic clean-up to ensure that the resources they contain are released as soon as possible. For more information, see the <xref:System.IDisposable?displayProperty=nameWithType> API reference and the [using statement](../statements/using.md) article.
For type instances that contain unmanaged resources, for example, a file handle, it's recommended to employ deterministic clean-up to ensure that the resources they contain are released as soon as possible. For more information, see the <xref:System.IDisposable?displayProperty=nameWithType> API reference and the [`using` statement](../statements/using.md) article.

## Operator overloadability

Expand Down
Loading

0 comments on commit b55fd6d

Please sign in to comment.