Skip to content

Commit

Permalink
update samples for stackalloc (dotnet#14)
Browse files Browse the repository at this point in the history
* update samples for stackalloc

Moved samples from the snippets folder in the dotnet/docs repo.

Add project file and main program to run snippets.

Add second sample that uses stackalloc initializers.

* respond to feedback.
  • Loading branch information
BillWagner authored and karelz committed Aug 31, 2018
1 parent e9c04f9 commit 3ef2d3c
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
12 changes: 12 additions & 0 deletions snippets/csharp/keywords/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace keywords
{
class Program
{
static void Main(string[] args)
{
StackAllocExamples.Examples();
}
}
}
102 changes: 102 additions & 0 deletions snippets/csharp/keywords/StackAllocExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;

namespace keywords
{
public static class StackAllocExamples
{
public static void Examples()
{
FibonacciGeneration();
Console.WriteLine("=========================");
InitializeBitMasks();
}

private static unsafe void InitializeBitMasks()
{
// <Snippet2>
int* mask = stackalloc[] {
0b_0000_0000_0000_0001,
0b_0000_0000_0000_0010,
0b_0000_0000_0000_0100,
0b_0000_0000_0000_1000,
0b_0000_0000_0001_0000,
0b_0000_0000_0010_0000,
0b_0000_0000_0100_0000,
0b_0000_0000_1000_0000,
0b_0000_0001_0000_0000,
0b_0000_0010_0000_0000,
0b_0000_0100_0000_0000,
0b_0000_1000_0000_0000,
0b_0001_0000_0000_0000,
0b_0010_0000_0000_0000,
0b_0100_0000_0000_0000,
0b_1000_0000_0000_0000
};

for (int i = 0; i < 16; i++)
Console.WriteLine(mask[i]);
/* Output:
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
*/

// </Snippet2>
}

private static unsafe void FibonacciGeneration()
{
// <Snippet1>
const int arraySize = 20;
int* fib = stackalloc int[arraySize];
int* p = fib;
// The sequence begins with 1, 1.
*p++ = *p++ = 1;
for (int i = 2; i < arraySize; ++i, ++p)
{
// Sum the previous two numbers.
*p = p[-1] + p[-2];
}
for (int i = 0; i < arraySize; ++i)
{
Console.WriteLine(fib[i]);
}
/* Output:
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
*/
// </Snippet1>
}
}
}
12 changes: 12 additions & 0 deletions snippets/csharp/keywords/keywords.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
</Project>

0 comments on commit 3ef2d3c

Please sign in to comment.