Unsafeクラスのサンプル的な奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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