Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1844.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ helpviewer_keywords:
author: NewellClark
dev_langs:
- CSharp
- VB
---
# CA1844: Provide memory-based overrides of async methods when subclassing 'Stream'

Expand Down Expand Up @@ -40,6 +39,10 @@ In order to realize these performance benefits, types that derive from <xref:Sys

The easiest way to fix violations is to rewrite your array-based implementation as a memory-based implementation, and then implement the array-based methods in terms of the memory-based methods.

## Example

:::code language="csharp" source="snippets/csharp/all-rules/ca1844.cs" id="snippet1":::

## When to suppress warnings

It's safe to suppress a warning from this rule if any of the following situations apply:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace ca1844
{
//<snippet1>
// This class violates the rule.
public class BadStream : Stream

Check failure on line 5 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(5,30): error CS0246: The type or namespace name 'Stream' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]
{
private readonly Stream _innerStream;

Check failure on line 7 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(7,26): error CS0246: The type or namespace name 'Stream' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]

public BadStream(Stream innerStream)

Check failure on line 9 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(9,26): error CS0246: The type or namespace name 'Stream' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]
{
_innerStream = innerStream;
}

public override bool CanRead => _innerStream.CanRead;
public override bool CanSeek => _innerStream.CanSeek;
public override bool CanWrite => _innerStream.CanWrite;
public override long Length => _innerStream.Length;
public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; }

public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Check failure on line 20 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(20,31): error CS0246: The type or namespace name 'Task<>' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]

Check failure on line 20 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(20,89): error CS0246: The type or namespace name 'CancellationToken' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]
{
// ...
return await _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
}

public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Check failure on line 26 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(26,31): error CS0246: The type or namespace name 'Task' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]

Check failure on line 26 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(26,85): error CS0246: The type or namespace name 'CancellationToken' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]
{
// ...
await _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
}

// Other required overrides
public override void Flush() => _innerStream.Flush();
public override int Read(byte[] buffer, int offset, int count) => _innerStream.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);

Check failure on line 35 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(35,48): error CS0246: The type or namespace name 'SeekOrigin' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]
public override void SetLength(long value) => _innerStream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) => _innerStream.Write(buffer, offset, count);
}

// This class satisfies the rule.
public class GoodStream : Stream

Check failure on line 41 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(41,31): error CS0246: The type or namespace name 'Stream' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]
{
private readonly Stream _innerStream;

Check failure on line 43 in docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1844.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\ca1844.cs(43,26): error CS0246: The type or namespace name 'Stream' could not be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\fundamentals\code-analysis\quality-rules\snippets\csharp\all-rules\all-rules.csproj]

public GoodStream(Stream innerStream)
{
_innerStream = innerStream;
}

public override bool CanRead => _innerStream.CanRead;
public override bool CanSeek => _innerStream.CanSeek;
public override bool CanWrite => _innerStream.CanWrite;
public override long Length => _innerStream.Length;
public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; }

public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
// ...
return await _innerStream.ReadAsync(buffer, cancellationToken);
}

public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
// ...
await _innerStream.WriteAsync(buffer, cancellationToken);
}

public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return await this.ReadAsync(buffer.AsMemory(offset, count), cancellationToken);
}

public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await this.WriteAsync(buffer.AsMemory(offset, count), cancellationToken);
}

// Other required overrides
public override void Flush() => _innerStream.Flush();
public override int Read(byte[] buffer, int offset, int count) => _innerStream.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);
public override void SetLength(long value) => _innerStream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) => _innerStream.Write(buffer, offset, count);
}
//</snippet1>
}