<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C#でExcelを読み込んで操作する方法を公開｜C# Excel操作テクニック.NET &#187; コンストラクタ</title>
	<atom:link href="http://excelcsharp.lance40.com/tag/tag-const/feed" rel="self" type="application/rss+xml" />
	<link>http://excelcsharp.lance40.com</link>
	<description>メソッドに貼り付けるだけで機能するソースコードを多数用意しています。</description>
	<lastBuildDate>Sun, 08 Nov 2015 04:41:39 +0000</lastBuildDate>
	<language>ja</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>コンストラクタとデストラクタ</title>
		<link>http://excelcsharp.lance40.com/clsconstdest.html</link>
		<comments>http://excelcsharp.lance40.com/clsconstdest.html#comments</comments>
		<pubDate>Sun, 17 Nov 2013 13:30:31 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[Excel操作クラス]]></category>
		<category><![CDATA[コンストラクタとデストラクタ]]></category>
		<category><![CDATA[Dispose]]></category>
		<category><![CDATA[コンストラクタ]]></category>
		<category><![CDATA[デストラクタ]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=81</guid>
		<description><![CDATA[「クラスを作成する」のページにも記載しましたが、クラスを操作する際にUsingの使用を可能にするため、作成するクラスにはコンストラクタとDisposeを実装します。 コンストラクタとデストラクタの概要についてはこちらに記 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>「<a title="クラスを作成する" href="http://excelcsharp.lance40.com/class.html">クラスを作成する</a>」のページにも記載しましたが、クラスを操作する際にUsingの使用を可能にするため、作成するクラスにはコンストラクタとDisposeを実装します。</p>
<p><a title="コンストラクタとデストラクタ" href="http://excelcsharp.lance40.com/cnstdst.html">コンストラクタとデストラクタの概要についてはこちら</a>に記述しました。使い分け方や各メソッドの意味など確認したい方は一読ください。</p>
<pre class="brush: csharp; title: ; notranslate">

#region &quot;コンストラクタとデストラクタ&quot;

        /// &lt;summary&gt;
        /// コンストラクタ
        /// &lt;/summary&gt;
        public ExcelManager()
        {
            xlApp = new Application();
        }

        /// &lt;summary&gt;
        /// 解放
        /// &lt;/summary&gt;
        public void Dispose()
        {
            // ExcelManagerが使われなくなったときに破棄する資源
            ReleaseExcelComObject(EnumReleaseMode.App);

            // dispose判定フラグON
            isDispose = true;

            // Dispose()が明示的に呼ばれたときは、GCからFinalize()呼び出しをさせない。
            GC.SuppressFinalize(this);
        }

        /// &lt;summary&gt;
        /// デストラクタ
        /// &lt;/summary&gt;
        ~ExcelManager()
        {
            // 呼出し側がDispose()してなかったらここでDispose
            if (isDispose == false)
            {
                Dispose();
            }
        }

#endregion

</pre>
<p><span style="font-size: medium;">■ コンストラクタ（public [クラス名]()）</span></p>
<p><strong>public [クラス名]() { 処理 }  </strong>はコンストラクタといい、ほかのクラスでNew()を宣言したときに呼ばれる処理です。</p>
<p>コンストラクタの処理では、クラス変数で宣言したxlAppのインスタンスを生成します。</p>
<p>&nbsp;</p>
<p><span style="font-size: medium;">■ デストラクタ（~[クラス名]()）</span></p>
<p><strong>~[クラス名]() { 処理 }</strong> は、コンストラクタとは対象に、クラスが破棄されるときに実行される処理です。しかし、C#ではガベージコレクタが機能しているため、ほとんどのクラスで実装する必要はありません。</p>
<p>明示的に解放が必要なリソースや、.NET Frameworkで管理されていないアンマネージリソースを使用するときに実装することがあります。</p>
<p>&nbsp;</p>
<p><span style="font-size: medium;">■ Dispose</span></p>
<p><strong>public void Dispose() { 処理 }</strong> は、クラスのインスタンスが破棄されるときに呼ばれる処理です。</p>
<p>デストラクタと混同してしまいがちですが、自動的に呼ばれる処理を記述するデストラクタに対して、Disposeは明示的に呼ぶ必要のある処理を記述します。</p>
<p>デストラクタはガベージコレクタが実行されたタイミングで呼ばれる処理のため実行されるタイミングが不明です。</p>
<p>通常はDisposeを実装して、ソースコード上では都度解放するようにしたほうが良いでしょう。</p>
<p>&nbsp;</p>
<p>クラスの準備は以上で完了です。</p>
<p>あとはプロパティ・メソッドカテゴリーから、必要な処理を移植すれば、Excelの操作が可能になります。</p>
]]></content:encoded>
			<wfw:commentRss>http://excelcsharp.lance40.com/clsconstdest.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
