{"id":3041,"date":"2025-03-23T05:06:05","date_gmt":"2025-03-23T05:06:05","guid":{"rendered":"https:\/\/histy.jp\/?p=3041"},"modified":"2025-03-23T05:07:12","modified_gmt":"2025-03-23T05:07:12","slug":"post-3041","status":"publish","type":"post","link":"https:\/\/histy.jp\/?p=3041","title":{"rendered":"MemberwiseClone\u3092\u8aac\u660e\u3059\u308b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6982\u8981<\/h2>\n\n\n\n<p>C#\u306b\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u3068\u3057\u3066\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u300cMemberwiseClone\u300d\u95a2\u6570\u3092\u7406\u89e3\u3057\u3066\u3001\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u305f\u3044\u65b9\u3078\u306e\u8a18\u4e8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7d50\u8ad6<\/h2>\n\n\n\n<p>MemberwiseClone\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092ShallowCopy\u3059\u308b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u578b\u3092\u77e5\u308d\u3046<\/h3>\n\n\n\n<p>C\uff03\u306b\u306f\u5024\u578b\u3068\u53c2\u7167\u578b\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<p>\u5024\u578b\u306f\u5024\u305d\u306e\u3082\u306e\u3092\u3001\u53c2\u7167\u578b\u306f\u53c2\u7167\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5024\u578b<\/h4>\n\n\n\n<p>\u5024\u578b\u306f\u3001<strong>\u30c7\u30fc\u30bf\u305d\u306e\u3082\u306e\u3092\u76f4\u63a5\u4fdd\u6301<\/strong>\u3057\u3001\u30e1\u30e2\u30ea\u4e0a\u306e <strong>\u30b9\u30bf\u30c3\u30af\uff08Stack\uff09<\/strong> \u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002<br>\u4ee3\u5165\u3084\u30b3\u30d4\u30fc\u6642\u306b\u306f <strong>\u5024\u305d\u306e\u3082\u306e\u304c\u30b3\u30d4\u30fc<\/strong> \u3055\u308c\u308b\u305f\u3081\u3001\u5143\u306e\u5909\u6570\u306b\u306f\u5f71\u97ff\u3092\u4e0e\u3048\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4ee3\u8868\u7684\u306a\u5024\u578b<\/strong><\/h5>\n\n\n\n<ul>\n<li><strong>\u57fa\u672c\u30c7\u30fc\u30bf\u578b<\/strong>\uff1aint, double, char, bool<\/li>\n\n\n\n<li><strong>\u69cb\u9020\u4f53\uff08struct\uff09<\/strong>\uff1aDateTime, Point \u306a\u3069<\/li>\n\n\n\n<li><strong>\u5217\u6319\u578b\uff08enum\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u5024\u578b\u306e\u52d5\u4f5c<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 10;\nint b = a; \/\/ \u5024\u304c\u30b3\u30d4\u30fc\u3055\u308c\u308b\n\nb = 20;\nConsole.WriteLine(a); \/\/ 10\uff08\u5143\u306e\u5024\u306f\u5909\u308f\u3089\u306a\u3044\uff09\nConsole.WriteLine(b); \/\/ 20<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u7167\u578b<\/h4>\n\n\n\n<p>\u53c2\u7167\u578b\u306f\u3001<strong>\u30c7\u30fc\u30bf\u305d\u306e\u3082\u306e\u3067\u306f\u306a\u304f\u3001\u30c7\u30fc\u30bf\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u30e1\u30e2\u30ea\u306e\u30a2\u30c9\u30ec\u30b9\uff08\u53c2\u7167\uff09\u3092\u4fdd\u6301<\/strong> \u3057\u307e\u3059\u3002<br>\u305d\u306e\u305f\u3081\u3001\u30e1\u30e2\u30ea\u4e0a\u306e <strong>\u30d2\u30fc\u30d7\uff08Heap\uff09<\/strong> \u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee3\u5165\u3084\u30b3\u30d4\u30fc\u6642\u306b\u306f <strong>\u53c2\u7167\uff08\u30a2\u30c9\u30ec\u30b9\uff09\u304c\u30b3\u30d4\u30fc<\/strong> \u3055\u308c\u308b\u305f\u3081\u3001\u8907\u88fd\u3057\u305f\u5909\u6570\u3092\u5909\u66f4\u3059\u308b\u3068\u5143\u306e\u5909\u6570\u3082\u5f71\u97ff\u3092\u53d7\u3051\u307e\u3059\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4ee3\u8868\u7684\u306a\u53c2\u7167\u578b<\/strong><\/h5>\n\n\n\n<ul>\n<li><strong>\u30af\u30e9\u30b9\uff08class\uff09<\/strong>\uff1astring, List, object \u306a\u3069<\/li>\n\n\n\n<li><strong>\u914d\u5217\uff08array\uff09<\/strong>\uff1aint[], string[]<\/li>\n\n\n\n<li><strong>\u30c7\u30ea\u30b2\u30fc\u30c8\uff08delegate\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>\u53c2\u7167\u578b\u306e\u52d5\u4f5c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n    public string Name;\n}\n\nPerson p1 = new Person { Name = \"Alice\" };\nPerson p2 = p1; \/\/ \u53c2\u7167\uff08\u30a2\u30c9\u30ec\u30b9\uff09\u304c\u30b3\u30d4\u30fc\u3055\u308c\u308b\n\np2.Name = \"Bob\"; \/\/ p2 \u3092\u5909\u66f4\n\nConsole.WriteLine(p1.Name); \/\/ \"Bob\"\uff08p1 \u3082\u5f71\u97ff\u3092\u53d7\u3051\u308b\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b3\u30d4\u30fc\u306b\u306f\u7a2e\u985e\u304c\u3042\u308b\u3053\u3068\u3092\u77e5\u308d\u3046<\/h3>\n\n\n\n<p>\u3042\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8907\u88fd\uff08\u30b3\u30d4\u30fc\uff09\u3057\u305f\u3044\u5834\u5408\u3001\u305d\u306e\u65b9\u6cd5\u306f2\u7a2e\u985e\u5b58\u5728\u3059\u308b\u3002<br>1\u3064\u76ee\u306f\u6d45\u3044\u30b3\u30d4\u30fc\u30012\u3064\u76ee\u306f\u6df1\u3044\u30b3\u30d4\u30fc\u3002<\/p>\n\n\n\n<p>\u3053\u306e\uff12\u3064\u3092\u4f7f\u3044\u5206\u3051\u308b\u969b\u306b\u3001\u53c2\u7167\u578b\u3092\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u6301\u3064\u304b\u304c\u91cd\u8981\u306b\u306a\u3063\u3066\u304f\u308b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6d45\u3044\u30b3\u30d4\u30fc\uff08Shallow Copy\uff09\u3068\u306f\uff1f<\/h4>\n\n\n\n<p><strong>\u300c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u307f\u3092\u30b3\u30d4\u30fc\u3059\u308b\u300d<\/strong> \u65b9\u6cd5\u3067\u3059\u3002<br>\u5024\u578b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306f<strong>\u5024\u304c\u30b3\u30d4\u30fc<\/strong>\u3055\u308c\u307e\u3059\u304c\u3001\u53c2\u7167\u578b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306f<strong>\u30a2\u30c9\u30ec\u30b9\uff08\u53c2\u7167\uff09\u3060\u3051\u304c\u30b3\u30d4\u30fc<\/strong>\u3055\u308c\u307e\u3059\u3002<br>\u3064\u307e\u308a\u3001<strong>\u30b3\u30d4\u30fc\u5f8c\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u5143\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u53c2\u7167\u578b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5171\u6709<\/strong> \u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Address\n{\n    public string City;\n}\n\nclass Person\n{\n    public string Name;  \/\/ \u5024\u578b\n    public int Age;      \/\/ \u5024\u578b\n    public Address Address; \/\/ \u53c2\u7167\u578b\n\n    public Person ShallowCopy()\n    {\n        return (Person)this.MemberwiseClone();\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Person original = new Person { Name = \"Alice\", Age = 25, Address = new Address { City = \"Tokyo\" } };\n        Person copy = original.ShallowCopy(); \/\/ \u6d45\u3044\u30b3\u30d4\u30fc\u3092\u4f5c\u6210\n\n        copy.Name = \"Bob\";        \/\/ \u30b3\u30d4\u30fc\u306e Name \u3092\u5909\u66f4\uff08\u5f71\u97ff\u306a\u3057\uff09\n        copy.Age = 30;            \/\/ \u30b3\u30d4\u30fc\u306e Age \u3092\u5909\u66f4\uff08\u5f71\u97ff\u306a\u3057\uff09\n        copy.Address.City = \"Osaka\"; \/\/ \u30b3\u30d4\u30fc\u306e Address.City \u3092\u5909\u66f4\uff08\u5f71\u97ff\u3042\u308a\uff09\n\n        Console.WriteLine(original.Name); \/\/ \"Alice\"\uff08\u5f71\u97ff\u306a\u3057\uff09\n        Console.WriteLine(original.Age);  \/\/ 25\uff08\u5f71\u97ff\u306a\u3057\uff09\n        Console.WriteLine(original.Address.City); \/\/ \"Osaka\"\uff08\u5f71\u97ff\u3092\u53d7\u3051\u305f\uff01\uff09\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u6d45\u3044\u30b3\u30d4\u30fc\u306e\u30dd\u30a4\u30f3\u30c8<\/strong><\/h5>\n\n\n\n<p>\u2714 <strong>\u5024\u578b\u30d5\u30a3\u30fc\u30eb\u30c9\uff08<code>Age<\/code>\uff09\u306f\u72ec\u7acb<\/strong>\u3057\u3066\u30b3\u30d4\u30fc\u3055\u308c\u308b\u305f\u3081\u3001\u5909\u66f4\u3057\u3066\u3082\u5f71\u97ff\u3057\u306a\u3044\u3002<br>\u274c <strong>\u53c2\u7167\u578b\u30d5\u30a3\u30fc\u30eb\u30c9\uff08<code>Address<\/code>\uff09\u306f\u5171\u6709<\/strong>\u3055\u308c\u308b\u305f\u3081\u3001\u30b3\u30d4\u30fc\u5148\u3067\u5909\u66f4\u3059\u308b\u3068\u5143\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3082\u5f71\u97ff\u3092\u53d7\u3051\u308b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6df1\u3044\u30b3\u30d4\u30fc\uff08Deep Copy\uff09\u3068\u306f\uff1f<\/h4>\n\n\n\n<p><strong>\u300c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u3059\u3079\u3066\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3001\u65b0\u3057\u3044\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u3057\u3066\u518d\u5e30\u7684\u306b\u30b3\u30d4\u30fc\u3059\u308b\u300d<\/strong> \u65b9\u6cd5\u3067\u3059\u3002<br>\u53c2\u7167\u578b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3082 <strong>\u65b0\u3057\u3044\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210<\/strong> \u3059\u308b\u305f\u3081\u3001\u30b3\u30d4\u30fc\u5f8c\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u5143\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u5b8c\u5168\u306b\u72ec\u7acb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Address\n{\n    public string City;\n\n    public Address DeepCopy()\n    {\n        return new Address { City = this.City }; \/\/ \u65b0\u3057\u3044 Address \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\n    }\n}\n\nclass Person\n{\n    public string Name;\n    public int Age;\n    public Address Address;\n\n    public Person DeepCopy()\n    {\n        return new Person\n        {\n            Name = this.Name,\n            Age = this.Age,\n            Address = this.Address.DeepCopy() \/\/ Address \u3082\u65b0\u3057\u304f\u4f5c\u6210\n        };\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Person original = new Person { Name = \"Alice\", Age = 25, Address = new Address { City = \"Tokyo\" } };\n        Person copy = original.DeepCopy(); \/\/ \u6df1\u3044\u30b3\u30d4\u30fc\u3092\u4f5c\u6210\n\n        copy.Name = \"Bob\";        \/\/ \u5f71\u97ff\u306a\u3057\n        copy.Age = 30;            \/\/ \u5f71\u97ff\u306a\u3057\n        copy.Address.City = \"Osaka\"; \/\/ \u5f71\u97ff\u306a\u3057\uff08\u72ec\u7acb\u3057\u3066\u3044\u308b\uff01\uff09\n\n        Console.WriteLine(original.Name); \/\/ \"Alice\"\uff08\u5f71\u97ff\u306a\u3057\uff09\n        Console.WriteLine(original.Age);  \/\/ 25\uff08\u5f71\u97ff\u306a\u3057\uff09\n        Console.WriteLine(original.Address.City); \/\/ \"Tokyo\"\uff08\u5f71\u97ff\u306a\u3057\uff01\uff09\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">MemberwiseClone\u3068\uff1d\u4ee3\u5165\u6f14\u7b97\u5b50\u306f\u7570\u306a\u308b<\/h3>\n\n\n\n<p>\u3042\u308c\uff1f\u3053\u308c\u306a\u3089\u65b0\u3057\u304f\u5909\u6570\u3092\u7528\u610f\u3057\u3066\u4ee3\u5165\u3059\u308c\u3070\u3088\u304f\u306a\u3044\uff1f\u3068\u601d\u3063\u305f\u65b9\u3002<br>\u305d\u308c\u306f\u9593\u9055\u3048\u3067\u3059\u3002<br>\u4ee3\u5165\u306f\u5024\u578b\u306f\u5024\u3092\u305d\u306e\u307e\u307e\u3001\u53c2\u7167\u578b\u306f\u53c2\u7167\u3092\u4ee3\u5165\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8a73\u3057\u3044\u8aac\u660e<\/h4>\n\n\n\n<p>\u53c2\u7167\u578b\u3092\u3001\u65b0\u3057\u3044\u5909\u6570\u306b<br>\uff11\uff0e\u4ee3\u5165\u3057\u305f\u5834\u5408<br>\uff12\uff0eMemberwiseClone\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u3001\u305d\u308c\u3092\u4ee3\u5165\u3057\u305f\u5834\u5408\u3002<\/p>\n\n\n\n<p>1\u306e\u5834\u5408\u306f\u53c2\u7167\u3092\u4ee3\u5165\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u53c2\u7167\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5024\u578b\u3092\u6301\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u4ee3\u5165\u5148\u306e\u5909\u6570\u304c\u3082\u3064\u5024\u578b\u3092\u5909\u66f4\u3059\u308b\u3068\u3001\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u5024\u3082\u5909\u66f4\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<p>\uff12\u306e\u5834\u5408\u306f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u81ea\u4f53\u306e\u53c2\u7167\u306f\u7570\u306a\u308b\u306e\u3067\u3001\u5024\u578b\u3092\u5909\u66f4\u3057\u3066\u3082\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u6301\u3064\u5024\u306f\u5909\u66f4\u3055\u308c\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u53c2\u7167\u5143<\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/ja-jp\/dotnet\/api\/system.object.memberwiseclone?view=net-9.0\">Object.MemberwiseClone \u30e1\u30bd\u30c3\u30c9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 C#\u306b\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u3068\u3057\u3066\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u300cMemberwiseClone\u300d\u95a2\u6570\u3092\u7406\u89e3\u3057\u3066\u3001\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u305f\u3044\u65b9\u3078\u306e\u8a18\u4e8b\u3002 \u7d50\u8ad6 MemberwiseClone\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092ShallowCopy\u3059\u308b\u3002 \u89e3\u8aac \u578b\u3092 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,9],"tags":[],"_links":{"self":[{"href":"https:\/\/histy.jp\/index.php?rest_route=\/wp\/v2\/posts\/3041"}],"collection":[{"href":"https:\/\/histy.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/histy.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/histy.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/histy.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3041"}],"version-history":[{"count":6,"href":"https:\/\/histy.jp\/index.php?rest_route=\/wp\/v2\/posts\/3041\/revisions"}],"predecessor-version":[{"id":3047,"href":"https:\/\/histy.jp\/index.php?rest_route=\/wp\/v2\/posts\/3041\/revisions\/3047"}],"wp:attachment":[{"href":"https:\/\/histy.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/histy.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/histy.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}