投稿数 - 437, コメント - 56317, トラックバック - 156

ref

メソッドの引数で結果を返したい場面というのは少なからずある。

// Class1 は構造体ではない。
Class1 c = new Class1();
bool success = Func( ref c );
if( success )
{
  c.Hoge();
}
private bool Func( ref Class1 c )
{
  c.Id = 1;
  return true;
}

C や C++ に慣れ親しんだ人が上記のように書いているのを時折見かけるが、このような ref は無意味だ。参照型で ref を指定するシナリオは 100% ない。ref を指定しなくても同じ結果を得られる。

private bool Func( Class1 c )
{
  c.Id = 1;
  return true;
}

これで呼び出し元に結果が返る。

次の例では呼び出し元に結果は返らない。

private bool Func( Class1 c )
{
  c = new Class1();
  c.Id = 1;
  return true;
}

この時は out を指定してやればよい。そうすれば呼び出し元でインスタンスを作ってやる必要がない。

Class1 c;
bool success = Func( out c );
if( success )
{
  c.Hoge();
}
private bool Func( out Class1 c )
{
  c = new Class1();
  c.Id = 1;
  return true;
}

唯一 ref を使わなければ出来ない用法が次だ。

private bool Func( ref Class1 c )
{
  int id = c.Id;
// id を何かに使う。


c = new Class1(); c.Id = 1; return true; }

これは絶対にやめた方がよい。
Func() メソッドは、意味のある引数を必要としながら、尚且つそれを別のインスタンスに置き換える。これでは Func() を使用するクライアントは混乱をきたすだろう。

つまり、参照型で ref を使う事はない。

投稿日時 : 2006年4月8日 21:06

フィードバック

# re: ref

私の場合、dotNet に来てから、

> メソッドの引数で結果を返したい場面というのは少なからずある。

これすらなかったりします。(^-^;)
2006/04/08 23:46 | じゃんぬ

# re: ref

>これすらなかったりします。(^-^;)
確かにそうですね。
気にせず戻り値でバンバン返せるのが嬉しい。
2006/04/10 0:14 | 囚人

# re: ref

>戻り値

ロストするとまずいアンマネージハンドル等を戻り値で返すと、変数で受け取る直前に非同期例外が発生すると色々まずかったりするので注意ですな。

// Func() から帰って、x に代入される直前に非同期例外が起きると、戻り値をロストする
X x = Func();

んで、.NET 2.0 からは Constrained Execution Regions (CER) に話が続くと。
2006/04/10 3:05 | NyaRuRu

# re: ref

TryParseとかはoutの用法ですな。
ただTryParseなんかもNullableで解決してほしかったんですが・・・
2006/04/10 22:53 | 中博俊

# re: ref

>んで、.NET 2.0 からは Constrained Execution Regions (CER) に話が続くと。
初めて聞く用語です。勉強せねば…。

>ただTryParseなんかもNullableで解決してほしかったんですが・・・
あ~、なるほど。どっちがいいんですかね。
2006/04/11 22:24 | 囚人

# Hi there! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I'm not very techincal but I can figure things out pretty quick. I'm thinking about creating my own but I'm not sure where to b

Hi there! This is kind of off topic but I need some advice from an established blog.
Is it difficult to set up your own blog? I'm
not very techincal but I can figure things out pretty quick.

I'm thinking about creating my own but I'm not sure where to begin. Do you have
any tips or suggestions? With thanks

# I always spent my half an hour to read this webpage's content all the time along with a cup of coffee.

I always spent my half an hour to read this webpage's content all the time
along with a cup of coffee.

# Hello there! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at options for another platform. I would be awesome

Hello there! I know this is kind of off topic but I was wondering which blog
platform are you using for this site? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hey there! Someone in my Myspace group shared this site with us so I came to give it a look. I'm definitely enjoying the information. I'm book-marking and will be tweeting this to my followers! Terrific blog and amazing design.

Hey there! Someone in my Myspace group shared this site with us so I came
to give it a look. I'm definitely enjoying the information. I'm book-marking
and will be tweeting this to my followers! Terrific blog
and amazing design.

# I every time spent my half an hour to read this website's content all the time along with a cup of coffee.

I every time spent my half an hour to read this website's content all the time along with a cup of coffee.

# You really make it seem so easy with your presentation but I to find this matter to be actually one thing that I believe I'd never understand. It sort of feels too complex and very large for me. I am looking forward on your next submit, I'll try to get t

You really make it seem so easy with your presentation but I to find this matter to be
actually one thing that I believe I'd never understand.
It sort of feels too complex and very large for me. I am looking forward on your
next submit, I'll try to get the dangle of it!

# jiXXBgbbpXbqOJ

http://imrdsoacha.gov.co/silvitra-120mg-qrms
2022/04/19 12:48 | johnansog

# What i don't realize is in reality how you are now not actually much more neatly-preferred than you may be now. You're very intelligent. You realize thus significantly when it comes to this matter, made me in my view believe it from numerous various ang

What i don't realize is in reality how you are now
noot actually much more neatly-preferred than you
may be now. You're very intelligent. Yoou realize thus significantly when iit comes to this matter,
made me in my view believe itt from numerous various angles.
Its like womern and men don't seem to be interested unless it's one thing to accomplish with Lady gaga!
Your individual stuffs outstanding. All the tume tazke care of
it up!

コメントの投稿

タイトル
名前
URL
コメント