質問するログイン新規登録

回答編集履歴

1

追記

2022/02/20 04:59

投稿

ozwk
ozwk

スコア13553

answer CHANGED
@@ -11,3 +11,22 @@
11
11
  if("hoge" == item){}//error CS0019: Operator `==' cannot be applied to operands of type `string' and `Item'
12
12
  ```
13
13
 
14
+
15
+ ---
16
+
17
+
18
+ ```C#
19
+ class Item{
20
+ public string Name{get;}
21
+ public Item(string name){
22
+ Name = name;
23
+ }
24
+ public static explicit operator Item(string s) => new Item(s);
25
+ // ... 等値比較など適宜実装
26
+ }
27
+ ```
28
+
29
+
30
+ ```C#
31
+ if("hoge" == (Item)"hoge"){} //error
32
+ ```