迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在 C# 中复制一个对象

作者:迹忆客 最近更新:2024/02/01 浏览次数:

本教程将介绍在 C# 中复制对象的方法。


在 C# 中使用 MemberWiseClone() 方法复制对象

不幸的是,没有内置的方法可以在 C# 中创建对象的单独副本。在下面的代码示例中证明了这种现象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace copy_an_object {
  class myClass {
    public String test;
  }
  class Program {
    static void Main(string[] args) {
      myClass a = new myClass();
      a.test = "This is a test";
      myClass b = a;
      b.test = "This is not a test";
      Console.WriteLine(a.test);
      Console.WriteLine(b.test);
    }
  }
}

输出:

This is not a test
This is not a test

我们两次都得到相同的输出,因为 C# 中的类的实例是指向特定内存位置的引用类型变量。由于 C# 的面向对象特性,新指针 b 也指向 a 的存储位置。

如果要为 b 创建一个单独的存储位置,则必须依靠某些用户定义的方法。MemberWiseClone() 方法用于在 C# 中创建调用对象的值的单独副本。MemberWiseClone() 函数的返回类型是 object。以下代码示例向我们展示了如何使用 C# 中的 MemberWiseClone() 函数创建类对象的单独副本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace copy_an_object {
  class myClass {
    public String test;
    public object Clone() {
      return this.MemberwiseClone();
    }
  }
  class Program {
    static void Main(string[] args) {
      myClass a = new myClass();
      a.test = "This is a test";
      myClass b = (myClass)a.Clone();
      b.test = "This is not a test";
      Console.WriteLine(a.test);
      Console.WriteLine(b.test);
    }
  }
}

输出:

This is a test
This is not a test

这次从输出中可以清楚地看到每个类对象都有一个单独的副本。我们在 myClass 类的 Clone() 函数内部实现了 MemberWiseClone() 函数。Clone() 函数返回类对象的单独副本。Clone() 函数的返回值被类型转换为 myClass,并由 b 类对象指向。


使用 C# 中的参数化构造方法复制对象

我们可以创建一个参数化的类构造函数以在 C# 中实现相同的目标。我们可以将先前的类对象传递给新类对象的构造函数,并从中复制所有值。以下代码示例向我们展示了如何使用 C# 中的参数化构造函数方法创建类对象的单独副本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace copy_an_object {
  class MyClass {
    public String test;

    public MyClass() {}

    public MyClass(MyClass other) {
      test = other.test;
    }
  }
  class Program {
    static void Main(string[] args) {
      MyClass a = new myClass();
      a.test = "This is a test";
      MyClass b = new MyClass(a);
      b.test = "This is not a test";
      Console.WriteLine(a.test);
      Console.WriteLine(b.test);
    }
  }
}

输出:

This is a test
This is not a test

我们为 MyClass 类创建了一个参数化的构造函数,该构造函数接受 MyClass 类的对象并从中复制 test 字符串。在主函数中,我们创建了 MyClass 类的对象 a,并将其传递给了 b 的构造函数。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便