R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

null 許容型の不思議

http://blogs.wankuma.com/rti/archive/2009/08/12/180078.aspx
僕はアリだと思うんだぜ

上記のエントリーのコメントで、trapemiya さんからいただいた話題です。

null 許容型を引数に持つメソッド

のオーバーロードで、メソッドに null を指定して呼び出したときの時に、どのメソッドが実行されるのかがわからないのです。

次のコードを実行すると、コンソールウィンドウに "int" と表示されます。


static void Main(string[] args)
{
  hoge(null);  
  Console.ReadLine();
}

static void hoge(double? a){
  Console.WriteLine("double");
}

static  void hoge(int? a){
  Console.WriteLine("int");
}


ちなみに、byte と int のオーバーロードだと、byte が実行されます。
当然ですが、次のコードのように、型をきちんと指定すれば double が実行されます。


static void Main(string[] args)
{
  double? a = null;
  hoge(a);  
  Console.ReadLine();
}


ということで、あえて直接 null を渡さなければ問題なさそうですが、この場合は、コンパイルエラーになって欲しい気もしますね。

投稿日時 : 2009年8月17日 18:50

Feedback

# re: null 許容型の不思議 2009/08/17 19:02 aetos

Nullable って構造体なら何でもいいんだよなーと思って、Guid? とかやるとオーバーロードが解決できなくてコンパイルエラーになりますね。
組み込み型の Nullable は特別扱いされてるっぽいですね。
ちなみに、明示的に Nullable<int> と Nullable<long> とか書いてもコンパイル通っちゃいました。

# re: null 許容型の不思議 2009/08/17 19:07 aetos

> どのメソッドが実行されるのかがわからないのです

一番サイズが小さいやつが呼ばれている気が。
double と long はどちらも 64bit ですが、その場合は long ですね。
あと、decimal、bool、enum は解決できませんでした。

# re: null 許容型の不思議 2009/08/17 19:13 aetos

んー? いろいろと組み合わせがあるみたいですね。
整数型(byte、short、int、long)とdecimalの組み合わせでは、整数型の方が呼ばれますね。
float、double、bool、enum と decimal の組み合わせではエラーになります。
表にまとめてみると何か法則性が見えるかも?

# re: null 許容型の不思議 2009/08/18 11:43 通りすがり

コンパイル後のソースを見てみると
static void Main(string[] args)
{
  hoge((int?)null);  
  Console.ReadLine();
}
と、勝手にint?にキャストされていました。
コンパイラの仕様を探る必要がありますね。

# re: null 許容型の不思議 2009/08/18 13:29 Orator

bool? が相手だと、競合するみたいですね。


// テストコード
static class Sample<A, B> where A : struct where B : struct
{
private static void Dump<T>(Nullable<T> p) where T : struct
{
Console.WriteLine(typeof(A).Name.PadLeft(8) + "? vs "
+ typeof(B).Name.PadLeft(8) + "? => " + typeof(T).Name + "?");
}
public static void Method(Nullable<A> p) { Dump(p); }
public static void Method(Nullable<B> p) { Dump(p); }
}

# re: null 許容型の不思議 2009/08/18 13:30 Orator

http://msdn.microsoft.com/ja-jp/library/bfft1t3c.aspx

// 実行結果

Byte? vs Decimal? => Byte?
Byte? vs Double? => Byte?
Byte? vs Single? => Byte?
Byte? vs Int32? => Byte?
Byte? vs UInt32? => Byte?
Byte? vs Int64? => Byte?
Byte? vs UInt64? => Byte?
Byte? vs Int16? => Byte?
Byte? vs UInt16? => Byte?
SByte? vs Decimal? => SByte?
SByte? vs Double? => SByte?
SByte? vs Single? => SByte?
SByte? vs Int32? => SByte?
SByte? vs Int64? => SByte?
SByte? vs Int16? => SByte?
Char? vs Decimal? => Char?
Char? vs Double? => Char?
Char? vs Single? => Char?
Char? vs Int32? => Char?
Char? vs UInt32? => Char?
Char? vs Int64? => Char?
Char? vs UInt64? => Char?
Char? vs UInt16? => Char?
Decimal? vs Int32? => Int32?
Decimal? vs UInt32? => UInt32?
Decimal? vs Int64? => Int64?
Decimal? vs UInt64? => UInt64?
Decimal? vs Int16? => Int16?
Decimal? vs UInt16? => UInt16?
Double? vs Single? => Single?
Double? vs Int32? => Int32?
Double? vs UInt32? => UInt32?
Double? vs Int64? => Int64?
Double? vs UInt64? => UInt64?
Double? vs Int16? => Int16?
Double? vs UInt16? => UInt16?
Single? vs Int32? => Int32?
Single? vs UInt32? => UInt32?
Single? vs Int64? => Int64?
Single? vs UInt64? => UInt64?
Single? vs Int16? => Int16?
Single? vs UInt16? => UInt16?
Int32? vs Int64? => Int32?
Int32? vs Int16? => Int16?
Int32? vs UInt16? => UInt16?
UInt32? vs Int64? => UInt32?
UInt32? vs UInt64? => UInt32?
UInt32? vs UInt16? => UInt16?
Int64? vs Int16? => Int16?
Int64? vs UInt16? => UInt16?
UInt64? vs UInt16? => UInt16?

# re: null 許容型の不思議 2009/08/18 20:04 R・田中一郎

aetos さん

>組み込み型の Nullable は特別扱いされてるっぽいですね。

うーむ。
あえてそうしているようにも思えますけどね。

>ちなみに、明示的に Nullable<int> と Nullable<long> とか書いてもコンパイル通っちゃいました。

でしょうねぇ。

>一番サイズが小さいやつが呼ばれている気が。

いくつか試してみて、僕も同じように思いました。

>double と long はどちらも 64bit ですが、その場合は long ですね。

僕が試した結果、整数型優先なのかな?、と思いました。

--------------------------------------------------
通りすがり さん

>コンパイル後のソースを見てみると
>と、勝手にint?にキャストされていました。

おおっ、わざわざ、そんなことしているんですね!
単純に、コンパイルエラーでいいじゃんwww

--------------------------------------------------
Orator さん

凄い・・・
Byte? が最強ですか^^;

# re: null 許容型の不思議 2009/08/18 23:04 Orator

表にまとめておきました。

http://www.vb-user.net/junk/replySamples/2009.08.18.14.35/Nullable.htm

# re: null 許容型の不思議 2009/08/19 10:00 trapemiya

>表にまとめておきました。

凄い・・・。なんかすごい資料になってますね。それにしてもよくわからないのでコネクトに出して見ようと思います。

# re: null 許容型の不思議 2009/08/19 10:28 trapemiya

トラックバックが飛んでないので手動で貼ります。

nullableの引数を持つメソッドをオーバーロードする場合に、引数にnullを与えた時にコンパイルエラーにならない。
http://blogs.wankuma.com/trapemiya/archive/2009/08/19/180274.aspx

# re: null 許容型の不思議 2009/08/19 18:24 R・田中一郎

Orator さん

>表にまとめておきました。

凄いw

---------------------------------
trapemiya さん
>凄い・・・。なんかすごい資料になってますね。それにしてもよくわからないのでコネクトに出して見ようと思います

これって、仕様じゃないかと思うんですよね。
わざわざ、キャストしているなんて、そうとしか思えない・・・
MSDN ライブラリーに、ソースが見つかれば済む話なんですけどね。

>トラックバックが飛んでないので手動で貼ります。

あいあい~

# ジェネリクの型推論 2009/08/20 15:39 R.Tanaka.Ichiro's Blog

ジェネリクの型推論

# vJlOiSzhjDPSlHpUp 2011/12/29 19:15 http://www.drpersky.com/treatments/rhinoplasty-los

Comrade kill yourself.

# AKWhbdDPvQlQUNxz 2011/12/29 19:48 http://www.seokiwi.com/

I serched through the internet and got here. What a wonderful invention of the mankind. With the help of the network you communicate, learn, read !... That helped us to get acquainted!...

# noFUVGDzMXuRXd 2012/01/07 7:18 http://www.luckyvitamin.com/c-1717-hcg

See it for the first time!!...

# バーバリー マフラー 2012/11/06 16:08 http://www.burberryfactory.com/バーバリー-マフラー-c-4.html

匿名なのに、私には誰だか分かる・・・(^_^;)ありがとう。。。

# Areilcts like this m 2016/04/19 18:43 Marv

Areilcts like this make life so much simpler.

# Hey! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2021/08/31 16:49 Hey! Do you know if they make any plugins to safeg

Hey! Do you know if they make any plugins to safeguard against hackers?

I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Hey! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2021/08/31 16:50 Hey! Do you know if they make any plugins to safeg

Hey! Do you know if they make any plugins to safeguard against hackers?

I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Hey! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2021/08/31 16:51 Hey! Do you know if they make any plugins to safeg

Hey! Do you know if they make any plugins to safeguard against hackers?

I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Hey! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2021/08/31 16:52 Hey! Do you know if they make any plugins to safeg

Hey! Do you know if they make any plugins to safeguard against hackers?

I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net. You actually know how to bring an issue to light and make it important. More and more people must look at this and understand this side of the story. 2021/09/02 3:49 Can I just say what a comfort to discover somebody

Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net.
You actually know how to bring an issue to light and make it important.
More and more people must look at this and understand this side of the story.
I can't believe you aren't more popular because you surely have the
gift.

# Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net. You actually know how to bring an issue to light and make it important. More and more people must look at this and understand this side of the story. 2021/09/02 3:50 Can I just say what a comfort to discover somebody

Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net.
You actually know how to bring an issue to light and make it important.
More and more people must look at this and understand this side of the story.
I can't believe you aren't more popular because you surely have the
gift.

# Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net. You actually know how to bring an issue to light and make it important. More and more people must look at this and understand this side of the story. 2021/09/02 3:51 Can I just say what a comfort to discover somebody

Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net.
You actually know how to bring an issue to light and make it important.
More and more people must look at this and understand this side of the story.
I can't believe you aren't more popular because you surely have the
gift.

# Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net. You actually know how to bring an issue to light and make it important. More and more people must look at this and understand this side of the story. 2021/09/02 3:52 Can I just say what a comfort to discover somebody

Can I just say what a comfort to discover somebody that truly knows what they're discussing on the net.
You actually know how to bring an issue to light and make it important.
More and more people must look at this and understand this side of the story.
I can't believe you aren't more popular because you surely have the
gift.

# This site certainly has all the information I needed concerning this subject and didn't know who to ask. 2021/09/02 21:20 This site certainly has all the information I need

This site certainly has all the information I needed concerning this subject and didn't know who to ask.

# This site certainly has all the information I needed concerning this subject and didn't know who to ask. 2021/09/02 21:21 This site certainly has all the information I need

This site certainly has all the information I needed concerning this subject and didn't know who to ask.

# This site certainly has all the information I needed concerning this subject and didn't know who to ask. 2021/09/02 21:22 This site certainly has all the information I need

This site certainly has all the information I needed concerning this subject and didn't know who to ask.

# This site certainly has all the information I needed concerning this subject and didn't know who to ask. 2021/09/02 21:23 This site certainly has all the information I need

This site certainly has all the information I needed concerning this subject and didn't know who to ask.

# Greetings! I know this is kinda off topic however , I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My blog goes over a lot of the same subjects as yours and I feel we could greatly 2021/09/04 19:27 Greetings! I know this is kinda off topic however

Greetings! I know this is kinda off topic however , I'd figured I'd ask.

Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa?
My blog goes over a lot of the same subjects as yours and I feel we could greatly
benefit from each other. If you are interested feel free to send me an email.
I look forward to hearing from you! Awesome blog by the
way!

# Greetings! I know this is kinda off topic however , I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My blog goes over a lot of the same subjects as yours and I feel we could greatly 2021/09/04 19:28 Greetings! I know this is kinda off topic however

Greetings! I know this is kinda off topic however , I'd figured I'd ask.

Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa?
My blog goes over a lot of the same subjects as yours and I feel we could greatly
benefit from each other. If you are interested feel free to send me an email.
I look forward to hearing from you! Awesome blog by the
way!

# Greetings! I know this is kinda off topic however , I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My blog goes over a lot of the same subjects as yours and I feel we could greatly 2021/09/04 19:29 Greetings! I know this is kinda off topic however

Greetings! I know this is kinda off topic however , I'd figured I'd ask.

Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa?
My blog goes over a lot of the same subjects as yours and I feel we could greatly
benefit from each other. If you are interested feel free to send me an email.
I look forward to hearing from you! Awesome blog by the
way!

# Greetings! I know this is kinda off topic however , I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My blog goes over a lot of the same subjects as yours and I feel we could greatly 2021/09/04 19:30 Greetings! I know this is kinda off topic however

Greetings! I know this is kinda off topic however , I'd figured I'd ask.

Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa?
My blog goes over a lot of the same subjects as yours and I feel we could greatly
benefit from each other. If you are interested feel free to send me an email.
I look forward to hearing from you! Awesome blog by the
way!

# Wonderful site you have here but I was curious if you knew of any discussion boards that cover the same topics discussed here? I'd really like to be a part of community where I can get suggestions from other experienced people that share the same intere 2021/09/12 13:25 Wonderful site you have here but I was curious if

Wonderful site you have here but I was curious if you knew
of any discussion boards that cover the same topics discussed here?
I'd really like to be a part of community where I
can get suggestions from other experienced people that share the same interest.
If you have any suggestions, please let me know. Many thanks!

quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Wonderful site you have here but I was curious if you knew of any discussion boards that cover the same topics discussed here? I'd really like to be a part of community where I can get suggestions from other experienced people that share the same intere 2021/09/12 13:26 Wonderful site you have here but I was curious if

Wonderful site you have here but I was curious if you knew
of any discussion boards that cover the same topics discussed here?
I'd really like to be a part of community where I
can get suggestions from other experienced people that share the same interest.
If you have any suggestions, please let me know. Many thanks!

quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Wonderful site you have here but I was curious if you knew of any discussion boards that cover the same topics discussed here? I'd really like to be a part of community where I can get suggestions from other experienced people that share the same intere 2021/09/12 13:27 Wonderful site you have here but I was curious if

Wonderful site you have here but I was curious if you knew
of any discussion boards that cover the same topics discussed here?
I'd really like to be a part of community where I
can get suggestions from other experienced people that share the same interest.
If you have any suggestions, please let me know. Many thanks!

quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Wonderful site you have here but I was curious if you knew of any discussion boards that cover the same topics discussed here? I'd really like to be a part of community where I can get suggestions from other experienced people that share the same intere 2021/09/12 13:28 Wonderful site you have here but I was curious if

Wonderful site you have here but I was curious if you knew
of any discussion boards that cover the same topics discussed here?
I'd really like to be a part of community where I
can get suggestions from other experienced people that share the same interest.
If you have any suggestions, please let me know. Many thanks!

quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# This post is genuinely a pleasant one it helps new web people, who are wishing in favor of blogging. https://parttimejobshiredin30minutes.wildapricot.org/ part time jobs hired in 30 minutes 2021/10/22 19:53 This post is genuinely a pleasant one it helps new

This post is genuinely a pleasant one it helps new web people, who are wishing in favor of blogging.

https://parttimejobshiredin30minutes.wildapricot.org/ part time jobs hired in 30 minutes

# Hi colleagues, how is all, and what you want to say about this article, in my view its really awesome in favor of me. 2021/11/14 12:54 Hi colleagues, how is all, and what you want to sa

Hi colleagues, how is all, and what you want to say about this article, in my view
its really awesome in favor of me.

タイトル  
名前  
Url
コメント