18. int a = 2;
int f(int x) => a * x;
int a = 2;
Func<int, int> f = x => a * x;
struct State
{
public int a;
}
static int f(int x, ref State s)
=> s.a * x;
class Anonymous
{
public int a;
public int f(int x, ref State s)
=> s.a * x;
}
19.
20.
21. var t = (x: 1, y: 2); int x = 1, y = 2;
var t = (x, y);
var z = t.Item1;
int x = 1, y = 2;
var t = (x, y);
var z = t.x;
int w = 1, y = 2;
var t = (w, y);
var z = t.w;
41. var s = "abcde";
ref IntPtr p = ref *(IntPtr*)(void*)&x;
p = (IntPtr)0; // 0 == null
Console.WriteLine(s);
p = (IntPtr)123456789; // 適当な値
Console.WriteLine(s);
× NullReferenceException
×
AccessViolationException
42. System.Runtime.CompilerServices.Unsafe
var s = "abcde";
ref IntPtr p = ref Unsafe.As<string, IntPtr>(ref s)
p = (IntPtr)0; // 0 == null
Console.WriteLine(s);
p = (IntPtr)123456789; // 適当な値
Console.WriteLine(s);
*(IntPtr*)(void*)&x
43. blittable struct Blittable
{
public int X;
public int Y;
}
unsafe ref U As<T, U>(ref T x)
where T : blittable
where U : blittable
=> ref *(T*)(void*)&x;
Blittable x;
ref long y = ref As<Blittable, long>(ref x);
44. struct Span<T>
{
ref T pointer;
int length;
}
struct Span<T>
{
ByReference<T> pointer;
int length;
}
45. ref int X(ref int x)
{
return ref x;
}
ref int X()
{
int x = 0;
return ref x;
}