えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

DisposeとClose

DisposeとCloseの関係で混乱が生じているようです。
http://blogs.wankuma.com/pinzolo/archive/2007/02/02/60433.aspx

.Net FrameworkのCloseは二通りの実装がされています。
これが混乱を招く原因の一つになっていると思います。

1)コンストラクタやほかのクラスで生成してCloseでDisposeも行うパターン
たとえばFileStreamクラスです。
File.OpenでFileStreamクラスが生成・OpenされFileStream.Closeで閉じます。
FileStreamクラスのコンストラクタで生成・OpenされFileStream.Closeで閉じることもできます。
FileStreamクラスにはOpenメソッドはありませんがCloseメソッドはあります、Close後に再オープンはできません。

アンマネージ リソースをクリーンアップするための Finalize および Dispose の実装 
http://msdn2.microsoft.com/ja-jp/library/b1yfkh5e(VS.80).aspx
このページで「Dispose を呼び出すパブリックな Close メソッドを作成します。」と書かれているのはこのパターンを意識したものです。

2)OpenとCloseが対になっていて、コンストラクタやDisposeとは関係ないパターン
たとえばSqlConnectionクラスです。
SqlConnectionクラスのコンストラクタで生成されDisposeされるまでは何度でもOpenとCloseが可能です。

投稿日時 : 2007年2月2日 16:40

コメントを追加

# re: DisposeとClose 2007/02/02 16:58 シャノン

結局のところ、クラスを使う側はその厄介なガイドラインは無視するべきなのでしょう。
「Closeは2通りある」のではなくて「クラスが100個あれば100通りあるかもしれない。そのうちいくつかはダブっているかもしれないけど」という気で臨まないと。
#Disposeの実装もね。

# re: DisposeとClose 2007/02/04 0:04 渋木宏明(ひどり)

クラスインスタンスがどうやって生成されたかどうかも「Dispose() するべきかどうか」とは無関係だと思うんですが。。。

# re: DisposeとClose 2007/02/04 13:57 えムナウ

「.Net FrameworkのCloseは二通り」なので自作クラスを作ったら100通りになるかもしれないですね。

「Dispose を呼び出すパブリックな Close メソッド」なのでOpenメソッドは作れないってだけだと思いますね。
「Dispose() するべきかどうか」とは無関係ですね。

# Very soon this website will be famous amid all blogging and site-building users, due to it's good content 2019/04/03 21:59 Very soon this website will be famous amid all blo

Very soon this website will be famous amid all blogging
and site-building users, due to it's good content

# It's nearly impossible to find experienced people in this particular topic, however, you sound like you know what you're talking about! Thanks 2019/04/09 8:15 It's nearly impossible to find experienced people

It's nearly impossible to find experienced people in this particular topic,
however, you sound like you know what you're talking about!
Thanks

# Good article. I am going through some of these issues as well.. 2019/07/02 0:21 Good article. I am going through some of these iss

Good article. I am going through some of these issues as well..

# Quality content is the important to invite the users to pay a quick visit the web site, that's what this site is providing. 2019/07/09 13:57 Quality content is the important to invite the use

Quality content is the important to invite the users to
pay a quick visit the web site, that's what this site is providing.

# Hi there! Someone in my Facebook group shared this website with us so I came to give it a look. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Excellent blog and outstanding design. 2019/08/14 11:51 Hi there! Someone in my Facebook group shared this

Hi there! Someone in my Facebook group shared this website with
us so I came to give it a look. I'm definitely
loving the information. I'm bookmarking and will be tweeting
this to my followers! Excellent blog and outstanding
design.

# Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging. thanks. 2022/03/23 4:58 Hi, yup this article is truly pleasant and I have

Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging.
thanks.

# Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging. thanks. 2022/03/23 4:59 Hi, yup this article is truly pleasant and I have

Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging.
thanks.

# Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging. thanks. 2022/03/23 5:00 Hi, yup this article is truly pleasant and I have

Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging.
thanks.

# Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging. thanks. 2022/03/23 5:01 Hi, yup this article is truly pleasant and I have

Hi, yup this article is truly pleasant and I have learned lot of things from it concerning blogging.
thanks.

# This page definitely has all of the information I needed about this subject and didn't know who to ask. 2022/06/05 22:06 This page definitely has all of the information I

This page definitely has all of the information I needed
about this subject and didn't know who to ask.

# Appreciation to my father who told me concerning this website, this web site is in fact awesome. 2022/06/10 21:19 Appreciation to my father who told me concerning t

Appreciation to my father who told me concerning this website, this
web site is in fact awesome.

# Howdy! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot! 2022/07/23 14:42 Howdy! I know this is somewhat off topic but I was

Howdy! I know this is somewhat off topic
but I was wondering if you knew where I could locate a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm
having difficulty finding one? Thanks a lot!

# Your style is unique compared to other folks I've read stuff from. I appreciate you for posting when you have the opportunity, Guess I'll just book mark this page. 2022/08/10 8:28 Your style is unique compared to other folks I've

Your style is unique compared to other folks I've read stuff from.
I appreciate you for posting when you have the opportunity, Guess I'll just book mark this page.

# I really like reading an article that will make men and women think. Also, thanks for allowing for me to comment! 2022/08/12 22:30 I really like reading an article that will make me

I really like reading an article that will make men and women think.

Also, thanks for allowing for me to comment!

# Hello, I enjoy reading all of your post. I like to write a little comment to support you. 2022/08/17 16:02 Hello, I enjoy reading all of your post. I like to

Hello, I enjoy reading all of your post. I like to write a little comment to support you.

# If you want to get a good deal from this article then you have to apply such strategies to your won website. 2022/09/07 13:38 If you want to get a good deal from this article t

If you want to get a good deal from this article then you have to apply such strategies to your
won website.

タイトル
名前
URL
コメント