diff --git a/snippets/standard/buffers/memory-t/owner-using/owner-using.cs b/snippets/standard/buffers/memory-t/owner-using/owner-using.cs new file mode 100644 index 00000000000..8a34500c94b --- /dev/null +++ b/snippets/standard/buffers/memory-t/owner-using/owner-using.cs @@ -0,0 +1,37 @@ +using System; +using System.Buffers; + +class Example +{ + static void Main() + { + using (IMemoryOwner<char> owner = MemoryPool<char>.Shared.Rent()) + { + Console.Write("Enter a number: "); + try { + var value = Int32.Parse(Console.ReadLine()); + + var memory = owner.Memory; + WriteInt32ToBuffer(value, memory); + DisplayBufferToConsole(memory.Slice(0, value.ToString().Length)); + } + catch (FormatException) { + Console.WriteLine("You did not enter a valid number."); + } + catch (OverflowException) { + Console.WriteLine($"You entered a number less than {Int32.MinValue:N0} or greater than {Int32.MaxValue:N0}."); + } + } + } + + static void WriteInt32ToBuffer(int value, Memory<char> buffer) + { + var strValue = value.ToString(); + + var span = buffer.Slice(0, strValue.Length).Span; + strValue.AsSpan().CopyTo(span); + } + + static void DisplayBufferToConsole(Memory<char> buffer) => + Console.WriteLine($"Contents of the buffer: '{buffer}'"); +} diff --git a/snippets/standard/buffers/memory-t/owner-using/owner-using.csproj b/snippets/standard/buffers/memory-t/owner-using/owner-using.csproj new file mode 100644 index 00000000000..06450524573 --- /dev/null +++ b/snippets/standard/buffers/memory-t/owner-using/owner-using.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>Visual_Studio_Projects</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/snippets/standard/buffers/memory-t/owner/owner.cs b/snippets/standard/buffers/memory-t/owner/owner.cs new file mode 100644 index 00000000000..0d6e8afc3a2 --- /dev/null +++ b/snippets/standard/buffers/memory-t/owner/owner.cs @@ -0,0 +1,43 @@ +using System; +using System.Buffers; + +class Example +{ + static void Main() + { + IMemoryOwner<char> owner = MemoryPool<char>.Shared.Rent(); + + Console.Write("Enter a number: "); + try { + var value = Int32.Parse(Console.ReadLine()); + + var memory = owner.Memory; + + WriteInt32ToBuffer(value, memory); + + DisplayBufferToConsole(owner.Memory.Slice(0, value.ToString().Length)); + } + catch (FormatException) { + Console.WriteLine("You did not enter a valid number."); + } + catch (OverflowException) { + Console.WriteLine($"You entered a number less than {Int32.MinValue:N0} or greater than {Int32.MaxValue:N0}."); + } + finally { + owner?.Dispose(); + } + } + + static void WriteInt32ToBuffer(int value, Memory<char> buffer) + { + var strValue = value.ToString(); + + var span = buffer.Span; + for (int ctr = 0; ctr < strValue.Length; ctr++) + span[ctr] = strValue[ctr]; + } + + static void DisplayBufferToConsole(Memory<char> buffer) => + Console.WriteLine($"Contents of the buffer: '{buffer}'"); + +} diff --git a/snippets/standard/buffers/memory-t/owner/owner.csproj b/snippets/standard/buffers/memory-t/owner/owner.csproj new file mode 100644 index 00000000000..06450524573 --- /dev/null +++ b/snippets/standard/buffers/memory-t/owner/owner.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>Visual_Studio_Projects</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/snippets/standard/buffers/memory-t/ownerless/ownerless.cs b/snippets/standard/buffers/memory-t/ownerless/ownerless.cs new file mode 100644 index 00000000000..27fe9a9279e --- /dev/null +++ b/snippets/standard/buffers/memory-t/ownerless/ownerless.cs @@ -0,0 +1,24 @@ +using System; + +class Example +{ + static void Main() + { + Memory<char> memory = new char[64]; + + Console.Write("Enter a number: "); + var value = Int32.Parse(Console.ReadLine()); + + WriteInt32ToBuffer(value, memory); + DisplayBufferToConsole(memory); + } + + static void WriteInt32ToBuffer(int value, Memory<char> buffer) + { + var strValue = value.ToString(); + strValue.AsSpan().CopyTo(buffer.Slice(0, strValue.Length).Span); + } + + static void DisplayBufferToConsole(Memory<char> buffer) => + Console.WriteLine($"Contents of the buffer: '{buffer}'"); +} diff --git a/snippets/standard/buffers/memory-t/ownerless/ownerless.csproj b/snippets/standard/buffers/memory-t/ownerless/ownerless.csproj new file mode 100644 index 00000000000..06450524573 --- /dev/null +++ b/snippets/standard/buffers/memory-t/ownerless/ownerless.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>Visual_Studio_Projects</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/snippets/standard/buffers/memory-t/task-returning/task-returning.cs b/snippets/standard/buffers/memory-t/task-returning/task-returning.cs new file mode 100644 index 00000000000..64db2b38888 --- /dev/null +++ b/snippets/standard/buffers/memory-t/task-returning/task-returning.cs @@ -0,0 +1,55 @@ +// <Snippet1> +using System; +using System.Buffers; +using System.IO; +using System.Threading.Tasks; + +public class Example +{ + // <Snippet1> + // An acceptable implementation. + static void Log(ReadOnlyMemory<char> message) + { + // Run in the background so that we don't block the main thread while performing IO. + Task.Run(() => { + StreamWriter sw = File.AppendText(@".\input-numbers.dat"); + sw.WriteLine(message); + sw.Flush(); + }); + } + // </Snippet1> + + // user code + public static void Main() + { + using (var owner = MemoryPool<char>.Shared.Rent()) + { + var memory = owner.Memory; + var span = memory.Span; + while (true) + { + int value = Int32.Parse(Console.ReadLine()); + if (value < 0) + return; + + int numCharsWritten = ToBuffer(value, span); + Log(memory.Slice(0, numCharsWritten)); + } + } + } + + private static int ToBuffer(int value, Span<char> span) + { + string strValue = value.ToString(); + int length = strValue.Length; + strValue.AsSpan().CopyTo(span.Slice(0, length)); + return length; + } +} +// </Snippet1> + +// Possible implementation of Log: + // private static void Log(ReadOnlyMemory<char> message) + // { + // Console.WriteLine(message); + // } diff --git a/snippets/standard/buffers/memory-t/task-returning/task-returning.csproj b/snippets/standard/buffers/memory-t/task-returning/task-returning.csproj new file mode 100644 index 00000000000..912509df752 --- /dev/null +++ b/snippets/standard/buffers/memory-t/task-returning/task-returning.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>void_returning</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/snippets/standard/buffers/memory-t/task-returning2/task-returning2.cs b/snippets/standard/buffers/memory-t/task-returning2/task-returning2.cs new file mode 100644 index 00000000000..f939082b7f7 --- /dev/null +++ b/snippets/standard/buffers/memory-t/task-returning2/task-returning2.cs @@ -0,0 +1,47 @@ +using System; +using System.Buffers; +using System.IO; +using System.Threading.Tasks; + +public class Example +{ + // <Snippet1> + // An acceptable implementation. + static Task Log(ReadOnlyMemory<char> message) + { + // Run in the background so that we don't block the main thread while performing IO. + return Task.Run(() => { + StreamWriter sw = File.AppendText(@".\input-numbers.dat"); + sw.WriteLine(message); + sw.Flush(); + }); + } + // </Snippet1> + + // user code + public static void Main() + { + using (var owner = MemoryPool<char>.Shared.Rent()) + { + var memory = owner.Memory; + var span = memory.Span; + while (true) + { + int value = Int32.Parse(Console.ReadLine()); + if (value < 0) + return; + + int numCharsWritten = ToBuffer(value, span); + Log(memory.Slice(0, numCharsWritten)); + } + } + } + + private static int ToBuffer(int value, Span<char> span) + { + string strValue = value.ToString(); + int length = strValue.Length; + strValue.AsSpan().CopyTo(span.Slice(0, length)); + return length; + } +} diff --git a/snippets/standard/buffers/memory-t/task-returning2/task-returning2.csproj b/snippets/standard/buffers/memory-t/task-returning2/task-returning2.csproj new file mode 100644 index 00000000000..176a92de088 --- /dev/null +++ b/snippets/standard/buffers/memory-t/task-returning2/task-returning2.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>void_returning</RootNamespace> + <LangVersion>latest</LangVersion> + </PropertyGroup> + +</Project> diff --git a/snippets/standard/buffers/memory-t/void-returning-async/void-returning-async.cs b/snippets/standard/buffers/memory-t/void-returning-async/void-returning-async.cs new file mode 100644 index 00000000000..b65d8d61eba --- /dev/null +++ b/snippets/standard/buffers/memory-t/void-returning-async/void-returning-async.cs @@ -0,0 +1,56 @@ +// <Snippet1> +using System; +using System.Buffers; +using System.IO; +using System.Threading.Tasks; + +public class Example +{ + // <Snippet1> + // An acceptable implementation. + static void Log(ReadOnlyMemory<char> message) + { + // Run in the background so that we don't block the main thread while performing IO. + Task.Run(() => { + string defensiveCopy = message.ToString(); + StreamWriter sw = File.AppendText(@".\input-numbers.dat"); + sw.WriteLine(defensiveCopy); + sw.Flush(); + }); + } + // </Snippet1> + + // user code + public static void Main() + { + using (var owner = MemoryPool<char>.Shared.Rent()) + { + var memory = owner.Memory; + var span = memory.Span; + while (true) + { + int value = Int32.Parse(Console.ReadLine()); + if (value < 0) + return; + + int numCharsWritten = ToBuffer(value, span); + Log(memory.Slice(0, numCharsWritten)); + } + } + } + + private static int ToBuffer(int value, Span<char> span) + { + string strValue = value.ToString(); + int length = strValue.Length; + strValue.AsSpan().CopyTo(span.Slice(0, length)); + return length; + } +} +// </Snippet1> + +// Possible implementation of Log: + // private static void Log(ReadOnlyMemory<char> message) + // { + // Console.WriteLine(message); + // } diff --git a/snippets/standard/buffers/memory-t/void-returning-async/void-returning-async.csproj b/snippets/standard/buffers/memory-t/void-returning-async/void-returning-async.csproj new file mode 100644 index 00000000000..912509df752 --- /dev/null +++ b/snippets/standard/buffers/memory-t/void-returning-async/void-returning-async.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>void_returning</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/snippets/standard/buffers/memory-t/void-returning/void-returning.cs b/snippets/standard/buffers/memory-t/void-returning/void-returning.cs new file mode 100644 index 00000000000..ed68c6be151 --- /dev/null +++ b/snippets/standard/buffers/memory-t/void-returning/void-returning.cs @@ -0,0 +1,43 @@ +// <Snippet1> +using System; +using System.Buffers; + +public class Example +{ + // implementation provided by third party + static extern void Log(ReadOnlyMemory<char> message); + + // user code + public static void Main() + { + using (var owner = MemoryPool<char>.Shared.Rent()) + { + var memory = owner.Memory; + var span = memory.Span; + while (true) + { + int value = Int32.Parse(Console.ReadLine()); + if (value < 0) + return; + + int numCharsWritten = ToBuffer(value, span); + Log(memory.Slice(0, numCharsWritten)); + } + } + } + + private static int ToBuffer(int value, Span<char> span) + { + string strValue = value.ToString(); + int length = strValue.Length; + strValue.AsSpan().CopyTo(span.Slice(0, length)); + return length; + } +} +// </Snippet1> + +// Possible implementation of Log: + // private static void Log(ReadOnlyMemory<char> message) + // { + // Console.WriteLine(message); + // } diff --git a/snippets/standard/buffers/memory-t/void-returning/void-returning.csproj b/snippets/standard/buffers/memory-t/void-returning/void-returning.csproj new file mode 100644 index 00000000000..912509df752 --- /dev/null +++ b/snippets/standard/buffers/memory-t/void-returning/void-returning.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.1</TargetFramework> + <RootNamespace>void_returning</RootNamespace> + </PropertyGroup> + +</Project>