2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

より意味が取りやすい代入可能判定

Posted at

参考

この記事は、以下の動画を参考にしています。
詳しくは、動画をご覧ください。

Type.IsAssignableTo

データ型Dが、データ型Bの変数に代入可能かを調べる

// BはDから代入可能か?
bool isAssignable = typeof(B).IsAssignableFrom(typeof(D));

IsAssignableFromは、思考の流れに逆らっており、意味が取りづらい。

.NET 5以降
// DはBへ代入可能か?
bool isAssignable = typeof(D).IsAssignableTo(typeof(B));

IsAssignableToの方が、意味が取りやすい。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?