Skip to content

Instantly share code, notes, and snippets.

@MeilCli
Created October 7, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeilCli/4621614d6435a2571a1eff7edc978d8a to your computer and use it in GitHub Desktop.
Save MeilCli/4621614d6435a2571a1eff7edc978d8a to your computer and use it in GitHub Desktop.
Unsafeクラスのサンプル的な奴
using System;
using System.Runtime.CompilerServices;
namespace UnsafeSample
{
public class Code
{
public void Add()
{
Console.WriteLine($"{nameof(Add)} Start");
var ar = new int[] { 0, 1, 2, 3, 4, 5 };
int i3 = Unsafe.Add(ref ar[0], 3);
Console.WriteLine($"0..5's offset 3 is {i3}"); // 3
Console.WriteLine($"{nameof(Add)} End");
}
public void AreSame()
{
Console.WriteLine($"{nameof(AreSame)} Start");
var a = new Object();
var b = new Object();
ref object ra1 = ref a;
ref object ra2 = ref a;
ref object rb = ref b;
Console.WriteLine($"{nameof(ra1)} and {nameof(ra2)} areSame: {Unsafe.AreSame(ref ra1, ref ra2)}"); // true
Console.WriteLine($"{nameof(ra1)} and {nameof(rb)} areSame: {Unsafe.AreSame(ref ra1, ref rb)}"); // false
Console.WriteLine($"{nameof(AreSame)} End");
}
public void As()
{
Console.WriteLine($"{nameof(As)} Start");
object a = "object";
ref object ra = ref a;
string ca = Unsafe.As<string>(a);
string cra = Unsafe.As<object, string>(ref ra);
Console.WriteLine($"{nameof(ca)}: {ca}, {nameof(cra)}: {cra}"); // object, object
Console.WriteLine($"{nameof(As)} End");
}
public unsafe void AsPointer()
{
Console.WriteLine($"{nameof(AsPointer)} Start");
var ar = new int[] { 0, 1, 2, 3, 4, 5 };
void* pointer = Unsafe.AsPointer(ref ar[0]);
int* intptr = (int*)pointer;
Console.WriteLine($"*({nameof(intptr)}+1) = {*(intptr + 1)}"); // 1
Console.WriteLine($"{nameof(AsPointer)} End");
}
public unsafe void AsRef()
{
Console.WriteLine($"{nameof(AsRef)} Start");
var ar = new int[] { 0, 1, 2, 3, 4, 5 };
fixed (int* intptr = &ar[0])
{
void* pointer = (void*)(intptr + 4);
int i4 = Unsafe.AsRef<int>(pointer);
Console.WriteLine($"{nameof(intptr)} + 4 = {i4}"); // 4
}
Console.WriteLine($"{nameof(AsRef)} End");
}
public void CopyBlock()
{
Console.WriteLine($"{nameof(CopyBlock)} Start");
var ar1 = new byte[] { 0, 1, 2, 3, 4, 5 };
var ar2 = new byte[ar1.Length];
Unsafe.CopyBlock(ref ar2[0], ref ar1[0], (uint)ar1.Length);
Console.WriteLine("Copied");
foreach (byte b in ar2)
{
Console.WriteLine(b); // 0, 1, 2, 3, 4, 5
}
Console.WriteLine($"{nameof(CopyBlock)} End");
}
public void InitBlock()
{
Console.WriteLine($"{nameof(InitBlock)} Start");
var ar = new byte[6];
Unsafe.InitBlock(ref ar[0], 3, (uint)ar.Length);
Console.WriteLine("Init");
foreach (byte b in ar)
{
Console.WriteLine(b); // 3, 3, 3, 3, 3, 3
}
Console.WriteLine($"{nameof(InitBlock)} End");
}
public unsafe void Read()
{
Console.WriteLine($"{nameof(Read)} Start");
int a = 10;
int* intptr = &a;
void* pointer = (void*)intptr;
Console.WriteLine($"Read intptr: {Unsafe.Read<int>(pointer)}"); // 10
Console.WriteLine($"{nameof(Read)} End");
}
public void SizeOf()
{
Console.WriteLine($"{nameof(SizeOf)} Start");
Console.WriteLine($"string size: {Unsafe.SizeOf<string>()}"); // 8 (in 64bit OS)
Console.WriteLine($"{nameof(SizeOf)} End");
}
}
}
using System;
using UnsafeSample;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var code = new Code();
code.Add();
Console.WriteLine();
code.AreSame();
Console.WriteLine();
code.As();
Console.WriteLine();
code.AsPointer();
Console.WriteLine();
code.AsRef();
Console.WriteLine();
code.CopyBlock();
Console.WriteLine();
code.InitBlock();
Console.WriteLine();
code.Read();
Console.WriteLine();
code.SizeOf();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment