R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

TryParse

TryParse

の戻り値は null 許容型で良いんじゃないだろうか?

int i = int.TryParse("あ") ?? 0;

int? x = int.TryParse("あ");
if (x == null) ・・・・

早速、拡張メソッドに加えてしまおう。

投稿日時 : 2008年3月7日 11:08

Feedback

# re: TryParse 2008/03/07 11:28 シャノン

TryじゃなくてParseの戻り値でいいんじゃない?

# re: TryParse 2008/03/07 11:33 επιστημη

僕は

int x;
if ( int.TryParse("あ",out x) ) {
 ...
}

の方がしっくりきますねぇ。
# 好みの問題でしかないんだけど。

# re: TryParse 2008/03/07 12:17 Blog Commenter(かずくん)

επιστημηさん
int x;
if ( int.TryParse("あ",out x) ) {
 ...
}


禿同

# re: TryParse 2008/03/07 12:23 ghost_shell

int?.Parse の方がいいのでは?

と思ったけど、そんなことできないのでは?

# 拡張メソッド 2008/03/07 16:28 myugaruの色々構想中・・・!

拡張メソッド

# re: TryParse 2008/03/07 16:43 myugaru

拡張メソッドはぁインスタンスからしか呼べませんw残念
(あ、また死語っぽいのつかってしまった・・)

# re: TryParse 2008/03/10 14:52 R・田中一郎

シャノン さん

>TryじゃなくてParseの戻り値でいいんじゃない?

そうですね。
Try だと bool を返すのが自然という気がしますもんね。

-------------------------------------------------------
επιστημη さん、かずくん

out で結果の値を取得することが、あまり好きではないんですよね。

-------------------------------------------------------
ghost_shell さん

>と思ったけど、そんなことできないのでは?

int.TryParse だと駄目ですね。
String の拡張メソッドになるかと思います。

こんな感じ?

public static Int32? ToInt32(this string value) {
Int32 r = 0;
if (Int32.TryParse(value, out r)) return r;
return null;
}

static void Main() {
var q = new[] {"あ".ToInt32(), "12".ToInt32(), "-345".ToInt32() };
q.ToList().ForEach(x => Console.WriteLine(x));
}

-------------------------------------------------------
myugaru さん

>拡張メソッドはぁインスタンスからしか呼べませんw残念

ん?、上記のサンプルコードは動きますよ?

# re: TryParse 2008/03/11 7:28 myugaru

To 田中一郎Rせんせい

>int.TryParse だと駄目ですね。
>String の拡張メソッドになるかと思います。

そうです。そういう意味です。

>ん?、上記のサンプルコードは動きますよ?

"あ"はインスタンスだからっす。
ありがとうございましたm(_)m

# re: TryParse 2008/03/11 7:30 myugaru

すみません。名前のみすたいぴんぐいたしましたorz

# re: TryParse 2008/03/11 9:06 R・田中一郎

ごめんなさい。
僕の元記事が端折りすぎでしたね。

# ブランド激安市場コピー 2016/02/27 5:28 m7cy2yl041

http://www.eevance.com/tokei/chopard/index.html
ロレックス買取"弊社はロレックスの商品特に大人気のロレックスデイトナシリーズのロレックス時計の種類を豊富に取り揃えます。日本ロレックス時計とロレックスレプリカのロレックスコピー品の品質よくて、激安税込み価格でご提供します。 }}}}}}

# Link exchange is nothing else except it is simply placing the other person's website link on your page at appropriate place and other person will also do similar in support of you. 2019/04/08 11:43 Link exchange is nothing else except it is simply

Link exchange is nothing else except it is simply placing the other person's website link
on your page at appropriate place and other person will also do similar in support of you.

# We are a group of volunteers and opening a new scheme in our community. Your web site provided us with useful information to work on. You've performed an impressive job and our entire community will be thankful to you. 2019/06/17 1:49 We are a group of volunteers and opening a new sch

We are a group of volunteers and opening a new scheme in our community.
Your web site provided us with useful information to work on. You've performed an impressive
job and our entire community will be thankful to you.

# It's amazing to pay a visit this site and reading the views of all mates about this post, while I am also keen of getting experience. 2019/07/30 21:47 It's amazing to pay a visit this site and reading

It's amazing to pay a visit this site and reading the views of all mates about this post, while I
am also keen of getting experience.

# It's amazing to pay a visit this site and reading the views of all mates about this post, while I am also keen of getting experience. 2019/07/30 21:48 It's amazing to pay a visit this site and reading

It's amazing to pay a visit this site and reading the views of all mates about this post, while I
am also keen of getting experience.

# It's amazing to pay a visit this site and reading the views of all mates about this post, while I am also keen of getting experience. 2019/07/30 21:49 It's amazing to pay a visit this site and reading

It's amazing to pay a visit this site and reading the views of all mates about this post, while I
am also keen of getting experience.

# It's amazing to pay a visit this site and reading the views of all mates about this post, while I am also keen of getting experience. 2019/07/30 21:50 It's amazing to pay a visit this site and reading

It's amazing to pay a visit this site and reading the views of all mates about this post, while I
am also keen of getting experience.

タイトル
名前
Url
コメント