たまに「じゃんぬねっと」が生存確認をする日記

しょーもない日記

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  983  : 記事  4  : コメント  34268  : トラックバック  277

ニュース

My Website

初心者向けのサイトです。

C# と VB.NET の入門サイト

最近のできごと

頑張っていたら年収が 4 桁万円 (残業はありません)。人生何があるかわかりません。

最高の配偶者、可愛い娘、ハンサムな息子と幸せな日々を送っています。
やっぱり世の中お金ですね!金!金!

ゴリラ?そんなの飼っていません!

Sponsored Link1

Sponsored Link2

Archive

書庫

前回の ICloneable と MemberwiseClone からの続きです。
シャローコピー (Shallow Copy) と、ディープコピー (Deep Copy) について書きます。

前回の記事で、System.Object.MemberwiseClone メソッドでクローン コピーする場合、
参照型のメンバについては、参照先のインスタンスそのものがコピーされるのではなく、
参照先を示すアドレスがコピーされるだけであるということを書きました。
つまり、コピー元もコピー先も結局は同じものを指し示していることを意味します。

このようなクローン コピーを、シャローコピー (Shallow Copy、簡易コピー、浅いコピーなど) と言います。
逆に、参照先のインスタンスをも複製する場合は、ディープコピー (Deep Copy、深いコピーなど) と言います。

こうやって書くと、シャローコピーが (「簡易コピー」という名称なので) 劣っているかのように見えますが、
シャローコピーを使うか、ディープコピーを使うかは臨機応変であり、劣っているわけではありません。

次に、MemberwiseClone メソッドを使ったシャローコピーと、ディープコピーの例を示します。
前回と同様、System.ICloneable インターフェイスを実装して、複製できることを明示します。

VB.NET

Option Strict On

' テスト用のデータクラス (System.ICloneable インターフェイスを実装)
Public Class WankumaMember : Implements System.ICloneable

    ' 面倒なのでフィールド メンバにしちゃってます
    Public MemberCode As Integer
    Public MemberName As String
    Public Age        As Integer
    Public Skills     As New System.Collections.ArrayList() '参照型
    Public Version    As New System.Version()               '参照型

    ' シャローコピー (Shallow Copy)
    Public Function ShallowCopy() As WankumaMember
        Return DirectCast(Me.Clone(), WankumaMember)
    End Function

    ' ディープコピー (Deep Copy)
    Public Function DeepCopy() As WankumaMember
        ' まずは簡易コピーしたものを格納
        Dim hWankuma As WankumaMember = DirectCast(Me.Clone(), WankumaMember)

        ' 参照型のメンバをクローン コピーする
        hWankuma.Skills  = DirectCast(Me.Skills.Clone(),  System.Collections.ArrayList)
        hWankuma.Version = DirectCast(Me.Version.Clone(), System.Version)

        Return hWankuma
    End Function

    ' System.ICloneable.Clone メソッド (非公開メンバとする)
    Private Function Clone() As Object Implements System.ICloneable.Clone
        Return Me.MemberwiseClone()
    End Function

End Class

シャローコピー側はそのままの実装で、MemberwiseClone メソッドの戻り値を同クラスの型に変換しているだけです。
ディープコピーについては、一旦簡易コピーした後に、参照型のメンバ (Skills、Version) をクローン コピーしています。

このようにオブジェクトのコピーは、手動によるコピーと、シャローコピー、ディープコピーがあります。
まあ、コレクションやデータクラスでないと普通は使わないとは思いますが、臨機応変に使い分けてください。

投稿日時 : 2006年4月7日 9:38

コメント

# re: シャローコピー と ディープコピー 2008/01/30 16:36 通りがかりの人
C#で下記のように記述したら、コンパイルエラー
// System.ICloneable.Clone メソッド (非公開メンバとする)
private object Clone()
{
return this.MemberwiseClone();
}

当たり前だけど、なぜVB.NETは通るだ?!

# re: シャローコピー と ディープコピー 2008/03/07 10:33 リック
MemberwiseClone が用意されているので、シャローコピーを実装する手間が省けていいですね(^-^)
Java にも追加されないかな?


# シャローコピー と ディープコピー 2013/11/02 11:38
上の二人って両方 バ カ だよな

# OCZHwNzNeeCkNKPSd 2019/06/29 0:38 https://www.suba.me/
euqoOC nfl jerseys than a toddler tea party. The boys are happy

# AxeoMpOUBGMAgyBe 2019/07/01 15:51 https://ustyleit.com/bookstore/downloads/fantastic
Thanks for the blog post.Thanks Again. click here

# PQQeQnpFKiKhgkd 2019/07/01 19:37 http://mazraehkatool.ir/user/Beausyacquise111/
thanks for sharing source files. many thanks

# eshgFJuZxjqywAawB 2019/07/04 0:49 http://www.authorstream.com/critlicelen/
Major thankies for the post.Thanks Again. Awesome.

# nFEZKmQKkwnRV 2019/07/04 5:05 http://bgtopsport.com/user/arerapexign483/
Outstanding post, I conceive people should acquire a lot from this website its rattling user genial. So much wonderful information on here .

# FQkkBDDTyDtQhTIZ 2019/07/04 17:20 https://bookmarkingworld.review/story.php?title=de
I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks!

# SxGlLhKwXREWNBZ 2019/07/04 17:27 http://dancermark44.pen.io
This very blog is obviously awesome and besides factual. I have picked up a bunch of helpful tips out of it. I ad love to go back again and again. Thanks a bunch!

# rWvikWnFnTpzGbubNC 2019/07/07 21:37 http://muscadepalaisroyal.com/2019/01/31/fresh-ess
Only a smiling visitant here to share the love (:, btw outstanding style and design. Reading well is one of the great pleasures that solitude can afford you. by Harold Bloom.

# OLvkQtmtNVmauldT 2019/07/08 17:02 http://bathescape.co.uk/
You are my breathing in, I own few web logs and rarely run out from to brand.

# JacZYeXbHXUBrx 2019/07/08 22:07 https://coolpot.stream/story.php?title=synacthen-d
When I open up your Feed it seems to be a ton of junk, is the issue on my part?

# RYMCOgzvbdMBLWMoBnj 2019/07/09 23:47 https://woodrestorationmag.com/blog/view/135345/th
Thanks again for the post.Really looking forward to read more. Much obliged.

# bDhMZVnaFjYJqBNF 2019/07/10 18:09 http://theyeslaptop.site/story.php?id=7796
pretty valuable material, overall I feel this is worth a bookmark, thanks

# BQgzeVaWUE 2019/07/10 23:18 http://bgtopsport.com/user/arerapexign958/
I went over this site and I conceive you have a lot of fantastic info, saved to favorites (:.

I truly appreciate this post. Want more.

# DigsWjVbUTWmozcoe 2019/07/11 23:05 https://www.philadelphia.edu.jo/external/resources
This site was how do you say it? Relevant!! Finally I have found something that helped me. Appreciate it!

# TzkfkmULbTKZNrG 2019/07/15 14:05 https://www.kouponkabla.com/marco-coupon-2019-get-
IaаАа?б?Т€Т?а?а?аАа?б?Т€Т?аБТ?d should talk to you here. Which is not some thing I do! I quite like reading a post which will make people believe. Also, numerous thanks permitting me to comment!

# YXRyUmDCESxcvqX 2019/07/15 15:40 https://www.kouponkabla.com/sand-and-sky-promo-cod
This very blog is without a doubt awesome and besides factual. I have found a lot of handy tips out of this source. I ad love to come back every once in a while. Thanks a lot!

Some genuinely quality posts on this web site , saved to my bookmarks.

# gFzzZuNpkFckUlYkW 2019/07/15 23:48 https://www.kouponkabla.com/cheaper-than-dirt-prom
Im obliged for the blog.Really looking forward to read more. Keep writing.

# RiHLiEkEJaBzZVLq 2019/07/16 1:41 https://www.caringbridge.org/visit/breakturnip70/j
Looking forward to reading more. Great post.Much thanks again. Much obliged.

Thanks for sharing this very good article. Very inspiring! (as always, btw)

# WguqNmvkiXNAtiq 2019/07/17 4:48 https://www.prospernoah.com/nnu-income-program-rev
scar treatment for acne scar treatment lotion scar treatment

# CQyIFzdZzjGqhGoTFZV 2019/07/17 6:32 https://www.prospernoah.com/clickbank-in-nigeria-m
This very blog is obviously educating and besides diverting. I have found a lot of handy stuff out of this amazing blog. I ad love to go back over and over again. Cheers!

# bHtRknosCZydBQ 2019/07/17 9:53 https://www.prospernoah.com/how-can-you-make-money
I truly like your weblog put up. Keep publishing far more valuable details, we value it!

# VvYqZbcAaJBQviz 2019/07/17 11:31 https://www.prospernoah.com/affiliate-programs-in-
You made some good points there. I looked on the internet for the topic and found most people will approve with your website.

# OFHLYnggKNVaumB 2019/07/17 20:03 http://haynes5417ap.savingsdaily.com/via-and-funky
I would like to uslysht just a little more on this topic

The reality is you ought to just stop smoking period and deal using the withdrawals. *I was quite happy to find this web-site.I wished to thanks for the time for this great read!!

# kaCIPxmBZhYXw 2019/07/18 1:20 http://dubaitravelerfoodghb.pacificpeonies.com/3k-
you write. The arena hopes for more passionate writers like you who aren at afraid to say how they believe. All the time follow your heart.

# fybjuxUFWlHS 2019/07/18 3:43 https://hirespace.findervenue.com/
Well I definitely liked studying it. This information provided by you is very useful for good planning.

# GgsDNKJVfnwjFMm 2019/07/18 5:26 http://www.ahmetoguzgumus.com/
Only a smiling visitor here to share the love (:, btw great design and style.

# KqkRnziGXAY 2019/07/18 10:34 http://als.anits.edu.in/members/bendersiegel995/
I think this is a real great article. Awesome.

Thanks for the blog post.Thanks Again. Really Great.

# mlijUOxkeXfMXYVEPe 2019/07/19 17:11 https://penzu.com/p/7799fd6a
Thanks for another wonderful article. Where else could anyone get that type of information in such a perfect way of writing? I ave a presentation next week, and I am on the look for such information.

We stumbled over here from a different web page and thought I might as well check things out. I like what I see so i am just following you. Look forward to checking out your web page repeatedly.

# lRwyfRfHIijUZiWo 2019/07/23 2:04 https://seovancouver.net/
I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

I think this is a real great post.Much thanks again. Want more.

# iSWkikIdEW 2019/07/23 7:02 https://seovancouver.net/
Identify who is posting about bag and the particular reason why you ought to be afraid.

# YCTCiOlMJZap 2019/07/23 8:41 http://events.findervenue.com/#Contact
This unique blog is no doubt educating as well as diverting. I have picked up a bunch of handy stuff out of this source. I ad love to return every once in a while. Cheers!

# ZknqfXgfEcWXBOrtm 2019/07/23 16:52 https://www.youtube.com/watch?v=vp3mCd4-9lg
pretty practical stuff, overall I feel this is really worth a bookmark, thanks

# jEsAQyvMTjHld 2019/07/23 22:50 https://www.nosh121.com/25-off-vudu-com-movies-cod
Informative article, just what I needed.

# xGfVtFSoRzlSfuCd 2019/07/24 3:54 https://www.nosh121.com/73-roblox-promo-codes-coup
Usually I do not comment in your weblog. I am additional in the silent sort but I wonder, is this wordpress since I am thinking of switching my own blog from blogspot to wordpress.

# DLzJiinvExJGybAb 2019/07/24 7:12 https://www.nosh121.com/93-spot-parking-promo-code
share. I know this is off topic but I simply needed to

# VbXcvrXPzyNrUJ 2019/07/24 10:37 https://www.nosh121.com/88-modells-com-models-hot-
This is all very new to me and this article really opened my eyes.Thanks for sharing with us your wisdom.

# oEJBeFKSuaPWIhtxfY 2019/07/24 17:49 https://www.nosh121.com/46-thrifty-com-car-rental-
Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Actually Wonderful. I am also an expert in this topic therefore I can understand your hard work.

# VoBJhEilvQoWBuRfgft 2019/07/24 21:30 https://www.nosh121.com/69-off-m-gemi-hottest-new-
There is noticeably a bundle to identify about this. I consider you made certain good points in features also.

# tNBhBuFxNxH 2019/07/25 2:12 https://seovancouver.net/
I really liked your article.Really looking forward to read more. Great.

# pFadEeZOBYp 2019/07/25 4:02 https://seovancouver.net/
Oakley Sunglasses Outlet Oakley Sunglasses Outlet

Looking forward to reading more. Great blog article.Thanks Again. Keep writing.

# iMvbYajzjzqt 2019/07/25 12:56 https://www.kouponkabla.com/cheggs-coupons-2019-ne
Thanks a lot for the blog post.Much thanks again. Keep writing.

# lmqDVSTApozFHd 2019/07/25 16:36 http://www.venuefinder.com/
There is noticeably a bundle to know about this. I feel you made some good points in features also.

# INbiVDHMbBBnq 2019/07/25 20:49 https://www.smore.com/5npxv-free-pc-apps-download-
Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Basically Fantastic. I am also an expert in this topic therefore I can understand your effort.

# kfvWyyoKTzCCKOZmnhF 2019/07/25 21:18 https://profiles.wordpress.org/seovancouverbc/
Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Basically Magnificent. I am also an expert in this topic therefore I can understand your hard work.

# qgpFBWaZzNdkBYnW 2019/07/25 23:08 https://www.facebook.com/SEOVancouverCanada/
Wow! This could be one particular of the most helpful blogs We ave ever arrive across on this subject. Basically Fantastic. I am also an expert in this topic so I can understand your effort.

# LCxowfiSSGqqe 2019/07/26 7:00 https://www.youtube.com/watch?v=FEnADKrCVJQ
Wow, great blog post.Thanks Again. Great.

# LpxUFTagbAaxAidALg 2019/07/26 8:50 https://www.youtube.com/watch?v=B02LSnQd13c
You can certainly see your skills in the work you write. The world hopes for more passionate writers like you who aren at afraid to say how they believe. Always follow your heart.

# NVnckRsCkNPRMx 2019/07/26 10:38 http://chessgiant6.edublogs.org/2019/07/24/rad-bat
wow, awesome blog.Thanks Again. Fantastic.

# fMhnhckylh 2019/07/26 13:59 https://profiles.wordpress.org/seovancouverbc/
that you wish be delivering the following. unwell unquestionably come further formerly again as exactly

# TqGBoSiDCWkx 2019/07/26 15:49 https://seovancouver.net/
Im thankful for the blog post. Want more.

# EVqnwfBHdFZYuJ 2019/07/26 18:50 https://www.couponbates.com/deals/noom-discount-co
Thanks for sharing, this is a fantastic post.

# GjkuwpvVzsUjutpObsV 2019/07/26 23:55 http://seovancouver.net/seo-vancouver-contact-us/
Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is wonderful, let alone the content!

You need to be a part of a contest for one of the most useful sites online. I am going to recommend this blog!

Lovely good %anchor%, We have currently put a different one down on my Xmas list.

# dKVBAXtqYLRD 2019/07/27 10:06 https://capread.com
Wow, great blog.Really looking forward to read more.

# ovEDmXBTlFOME 2019/07/27 13:40 http://bumprompak.by/user/eresIdior850/
Perfectly written written content , regards for selective information.

# mmfBRHSrTdHQDSHEsx 2019/07/27 19:28 https://www.nosh121.com/80-off-petco-com-grooming-
The thing i like about your weblog is that you generally post direct for the point info.:,*`,

Im grateful for the blog article.Thanks Again. Much obliged.

This is one awesome blog.Much thanks again. Want more.

# nNxkIOXsIclGYp 2019/07/28 23:56 https://twitter.com/seovancouverbc
I will immediately grab your rss feed as I can not find your e-mail subscription link or e-newsletter service. Do you have any? Please let me know in order that I could subscribe. Thanks.

# FLOxdTQHRtypbLqyH 2019/07/29 2:22 https://twitter.com/seovancouverbc
This information is priceless. When can I find out more?

# pqBKumPeZXdNlHgH 2019/07/29 9:29 https://www.kouponkabla.com/noodles-and-company-co
It as especially a abundant as thriving as practical a part of details. I will live thankful that you just free this type of information as anyway as us all.

# MpLEKqOUPbxnX 2019/07/30 6:39 https://www.kouponkabla.com/erin-condren-coupons-2
Spot on with this write-up, I truly think this website needs much more consideration. I all probably be again to read much more, thanks for that info.

# htfOFUUkQDXox 2019/07/30 14:49 https://twitter.com/seovancouverbc
Thanks a lot for this kind of details I had been exploring all Yahoo to locate it!

# KqSuEozIbjkicPBXo 2019/07/30 15:52 https://www.kouponkabla.com/coupon-code-for-viral-
This web site definitely has all of the information and facts I wanted about this subject and didn at know who to ask.

# dgXhaIFarBEBnhJV 2019/07/30 18:20 https://mysp.ac/4Q9uf
Tumblr article I saw someone writing about this on Tumblr and it linked to

# wZmCyaNtQXltnvWs 2019/07/30 19:51 http://seovancouver.net/what-is-seo-search-engine-
Im grateful for the blog.Really looking forward to read more. Much obliged.

# XbnlIjXETkzdHNTsxNc 2019/07/31 0:51 http://twohoursrecipes.pro/story.php?id=7594
More about the author Why does Firefox not work since I downloaded yahoo instant messenger?

Im obliged for the article.Really looking forward to read more. Want more.

# kZMEkyqrZxy 2019/07/31 7:40 http://qfod.com
Wow, that as what I was looking for, what a stuff! present here at this website, thanks admin of this site.

Major thankies for the blog post.Really looking forward to read more. Much obliged.

# kdjmrqQpJyTBcsT 2019/07/31 19:40 http://europeanaquaponicsassociation.org/members/b
Spot on with this write-up, I truly feel this web site needs a lot more attention. I all probably be back again to read through more, thanks for the advice!

Looking forward to reading more. Great article.Thanks Again. Great.

some pics to drive the message home a little bit, but instead of that, this is great blog.

# ETvgfHYygDq 2019/08/06 21:13 http://nifnif.info/user/Batroamimiz290/
pretty practical stuff, overall I consider this is really worth a bookmark, thanks

# bYuedFcxichGQlG 2019/08/07 3:39 https://seovancouver.net/
Wow, great article post.Really looking forward to read more. Awesome.

# JiLKZosIiwBkyqeeUd 2019/08/07 8:36 https://tinyurl.com/CheapEDUbacklinks
safe power leveling and gold I feel extremely lucky to have come across your entire web pages and look forward to plenty of more exciting minutes reading here

# RctVWoonlGaAVGXjtJS 2019/08/07 10:32 https://www.egy.best/
Looking forward to reading more. Great blog.Thanks Again. Awesome.

# evwBsVLQqWVJJxAY 2019/08/07 12:33 https://www.bookmaker-toto.com
This excellent website certainly has all of the information I needed concerning this subject and didn at know who to ask.

# dJERxOkZyYNaVnnt 2019/08/07 14:36 https://seovancouver.net/
Really informative blog.Really looking forward to read more. Awesome.

# RtPhFlAQaNDW 2019/08/07 16:40 https://www.onestoppalletracking.com.au/products/p
You have brought up a very great points, thanks for the post.

# QpZXSwOWgGZ 2019/08/08 5:12 http://gamalightscolors.pro/story.php?id=29861
There as definately a lot to find out about this subject. I love all of the points you ave made.

# sXPmyGiquXDOdStMmWC 2019/08/08 9:15 http://tennisgrass15.iktogo.com/post/london-remova
Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is wonderful, as well as the content!

# cjCnwYcgIARvtHcmd 2019/08/08 13:18 http://gayvids.club/story.php?id=26237
Ridiculous quest there. What occurred after? Thanks!

# VbvKhghrdoRmBrFgKC 2019/08/08 19:18 https://seovancouver.net/
Thanks-a-mundo for the article post. Want more.

# qlRosWsfQbERlfkPRy 2019/08/10 0:00 https://seovancouver.net/
This is a great tip particularly to those fresh to the blogosphere. Short but very accurate information Appreciate your sharing this one. A must read article!

# DzNOpWvDHnmrFLX 2019/08/12 18:08 https://www.youtube.com/watch?v=B3szs-AU7gE
Very good article post.Much thanks again. Fantastic.

# AZvrYHSGUZfM 2019/08/13 0:37 https://seovancouver.net/
pretty handy stuff, overall I think this is well worth a bookmark, thanks

# GrRaNPBYDVsbZOUdC 2019/08/13 10:43 https://app.emailmeform.com/builder/account
This very blog is no doubt educating additionally factual. I have discovered a lot of handy tips out of it. I ad love to come back again soon. Thanks a bunch!

# NowoFjnJesuuHTm 2019/08/13 19:36 http://theclothingoid.club/story.php?id=10463
You are my aspiration , I have few blogs and often run out from to post.

# utZZOkLXRscrj 2019/08/14 4:19 https://www.smashwords.com/profile/view/Hicess
Utterly indited written content , regards for information.

# oNgrPuZObIXqQ 2019/08/15 5:28 http://www.geati.ifc-camboriu.edu.br/wiki/index.ph
Really enjoyed this blog.Much thanks again. Fantastic.

It was hard It was hard to get a grip on everything, since it was impossible to take in the entire surroundings of scenes.

# KsNWhiMWAPjoeuDfEZ 2019/08/16 21:43 https://www.prospernoah.com/nnu-forum-review/
your articles. Can you recommend any other blogs/websites/forums that cover the same subjects?

# PFESrGRoucSh 2019/08/17 3:50 https://www.zotero.org/AlessandroBlevins
This is one awesome post.Much thanks again. Great.

Post Feedback

タイトル
名前
Url:
コメント: