4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# で 参照型から参照値を表示したい

Posted at

というわけでタイトルの通り、参照型から参照値にあたる値を取得したいと思います。

具体的には Unsafe.As<T,nint>(ref T) で取得することができます。

nintIntPtr の 数値表現名 (内部的にはIntPtr)

using System;
using System.Runtime.CompilerServices;

var text1 = "test";
Console.WriteLine($"{nameof(text1),-10}: {ToRefValue(ref text1):X16}");
// 同じリテラルは 同じインスタンスが使われる
var text2 = "test";
Console.WriteLine($"{nameof(text2),-10}: {ToRefValue(ref text2):X16}");
// 明示的に new すると別のインスタンスが使われる
string text3 = new(text1);
Console.WriteLine($"{nameof(text3),-10}: {ToRefValue(ref text3):X16}");
// object
object obje4 = new();
Console.WriteLine($"{nameof(obje4),-10}: {ToRefValue(ref obje4):X16}");
// 代入では参照値は変わらない
var obje5 = obje4;
Console.WriteLine($"{nameof(obje5),-10}: {ToRefValue(ref obje5):X16}");

nint ToRefValue<T>(ref T t) where T:class => Unsafe.As<T,nint>(ref t);

playground

text1     : 000001DB5AC6FDA8
text2     : 000001DB5AC6FDA8
text3     : 000001DB5ACB7540
obje4     : 000001DB5ACB75C8
obje5     : 000001DB5ACB75C8

まぁ、参照値が取れたからといって 実務には特に関係ないというかうれしいこと少ないのですが、レクチャーする際とかに必要があればこうやって取得する方法はないわけではないということで。

以上。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?