Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:05:38+00:00 2026-05-26T05:05:38+00:00

Can somebody please help me to write a DeepCopy routine for this matrix class

  • 0

Can somebody please help me to write a DeepCopy routine for this matrix class I have ? I dont have a great deal of experience in C#.

public class Matrix<T>
{
    private readonly T[][] _matrix;
    private readonly int row;
    private readonly int col;

    public Matrix(int x, int y)
    {
        _matrix = new T[x][];
        row = x;
        col = y;
        for (int r = 0; r < x; r++)
        {
            _matrix[r] = new T[y];
        }
    }
}

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:05:39+00:00Added an answer on May 26, 2026 at 5:05 am

    The simplest way of deep copying would be using some kind of serializer (for instance BinaryFormatter), but this requires not only your type to be decorated as Serializable, but also the type T.

    An example implementation of that could be:

    [Serializable]
    public class Matrix<T>
    {
      // ...
    }
    
    public static class Helper
    {
      public static T DeepCopy<T>(T obj)
      {
        using (var stream = new MemoryStream())
        {
          var formatter = new BinaryFormatter();
          formatter.Serialize(stream, obj);
          stream.Position = 0;
          return (T) formatter.Deserialize(stream);
        }
      }
    }
    

    The issue here, is that you have no control over which type is supplied as generic type parameter. Without knowing more about which kind of types you wish to clone, an option might be to put a generic type constraint on T to only accept types that implement ICloneable.

    In which case you could clone Matrix<T> like so:

    public class Matrix<T> where T: ICloneable
    {
      // ... fields and ctor
    
      public Matrix<T> DeepCopy()
      {
        var cloned = new Matrix<T>(row, col);
        for (int x = 0; x < row; x++) {
          for (int y = 0; y < col; y++) {
            cloned._matrix[x][y] = (T)_matrix[x][y].Clone();
          }
        }
        return cloned;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody please help me with this mail script. I'm simply trying to send
I'm trying to optimize this mysql query using EXPLAIN. Can somebody please help me
Would somebody help me please in this question. I'm new in Matlab... And it's
can somebody tell me how to use this class timers from python in my
Can somebody please help me understand why my custom JComponent 'Bar', only displays when
I want to replace the second line file content, can somebody help please based
Can somebody please help me out on how to increase the size of the
I hope this is the right place to post this and somebody can help.
Can somebody please help me to port the following code to GCC ? I
Hello Experts can somebody please indentify the problem with this server why this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.