Garbage Collection

塵も積もれば山

目次

Blog 利用状況

ニュース

C++とかC#とか数学ネタを投下していく予定です。

[その他のページ]
日々の四方山話を綴った日記出水の日記帳

書庫

日記カテゴリ

[C#]ユージングインパクト

C#のusingは便利ですが、ネストが深くなるという問題もあります。
そう愚痴ると、同時に宣言するなら並べて書くことができる、という
解決策を提案をされることがよくあります。

// ネストが深いなぁ…
using(var a = new ClassA()){
  using(var b = new ClassB()){
    using(var c = new ClassC()){
      ...
    }
  }
}
// こうすると、ネストが1段で済む
using(var a = new ClassA())
using(var b = new ClassB())
using(var c = new ClassC()){
  ...
}

だけど、よーく考えると以下の書き方はあまりしませんよね。

for (int x = 0; x < 30; x++)
for (int y = 0; y < 30; y++)
for (int z = 0; z < 30; z++){
  ...
}
if (a == 0)
if (b == 0)
if (c == 0){
  ...
}

途端に冒頭の解決策が不安になってきます。

投稿日時 : 2008年9月29日 20:31

Feedback

# re: [C#]ユージングインパクト 2008/09/29 20:49 おのでら

using が2個以上のときは最初の書き方(1段バージョン)を使ってますよー

# re: [C#]ユージングインパクト 2008/09/29 21:32 NyaRuRu

>だけど、よーく考えると以下の書き方はあまりしませんよね。

案外、言語次第じゃないかという気もします。
C#でもクエリ式ではこのような書き方が普通ですね。

参考:
http://d.hatena.ne.jp/ufcpp/20080304#1204632832

# re: [C#]ユージングインパクト 2008/09/29 21:53 れい

> だけど、よーく考えると以下の書き方はあまりしませんよね。

あれ?
しまくりですが。

# Using 一方VBは 2008/09/29 23:25 katamari.wankuma.com

Using 一方VBは

# re: [C#]ユージングインパクト 2008/09/29 23:50 aetos

for ではしませんが using ではやりますねぇ。

# re: [C#]ユージングインパクト 2008/09/30 1:30 R・田中一郎

using は、インスタンス生成のための構文なので、for や if のような、その後の挙動が記述されたブロックのインデントよりも、気軽に揃えて記述してしまっても問題ないんじゃないかと思ってます。

# re: [C#]ユージングインパクト 2008/10/01 22:39 出水

絶望した!
この流れで誰もC++/CLIを出さないのに絶望した!!

ま、usingを並べて書けるのが今まで特殊な構文だと思ってたら
実は普通の構文だったなーというのに気づいたというだけのエントリーです

# XlcRWcVVKThXOeKG 2011/12/16 2:10 http://www.healthinter.org/health/page/erythromyci

As usual, the webmaster posted correctly..!

# GrrutsNyFYItOUC 2011/12/26 23:44 http://www.discreetpharmacist.com/ger/index.asp

Internet is written with the capital letter in a sentence, by the way. And hundredths are written not with a point but with a comma. This is according to the standard. And actually everything is very good..!

# dTBzBWvTRrJIxkqlG 2011/12/27 6:39 http://www.instawares.com/

I must admit, the webmaster is a cool guy..!

# rAWdgIKyQpCFjy 2011/12/27 19:55 http://www.instawares.com/

I do`t regret that spent a few of minutes for reading. Write more often, surely'll come to read something new!...

# SbEyGSzvzL 2012/01/07 3:17 http://www.luckyvitamin.com/c-1181-arthritic-condi

Edidn`t think about that. I'll tell my mother, she won`t believe it..!

# Fastidious response in return of this query with firm arguments and describing all on the topic of that. 2018/10/05 0:48 Fastidious response in return of this query with f

Fastidious response in return of this query with firm arguments and describing all on the topic of
that.

# Whats up very cool web site!! Man .. Excellent .. Wonderful .. I will bookmark your web site and take the feeds also? I'm satisfied to find numerous helpful information right here in the put up, we want work out extra strategies on this regard, thank yo 2018/11/19 12:07 Whats up very cool web site!! Man .. Excellent ..

Whats up very cool web site!! Man .. Excellent ..
Wonderful .. I will bookmark your web site and take the feeds also?
I'm satisfied to find numerous helpful information right here in the put
up, we want work out extra strategies on this regard, thank
you for sharing. . . . . .

タイトル
名前
Url
コメント