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 4622446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:50:40+00:00 2026-05-22T02:50:40+00:00

class CloneExample : ICloneable { private int m_data ; private double m_data2 ; public

  • 0
class CloneExample : ICloneable
{
    private int m_data ;
    private double m_data2 ;

    public object Clone()
    {
        return this.MemberwiseClone();
    }

    public CloneExample()
    {

    }

    public CloneExample(int data1, int data2)
    {
        m_data = data1;
        m_data2 = data2;
    }

    public override string ToString()
    {
       return String.Format("[d1,d2] {0} {1}", m_data, m_data2);
    }
}
class Program
{
    static void Main(string[] args)
    {
        CloneExample aEx = new CloneExample(10,20);
        CloneExample aEx2 = (CloneExample)aEx.Clone();

        Console.WriteLine("the first object value is {0}", aEx.ToString());
        Console.WriteLine("the first object value is {0}", aEx2.ToString());

        Console.Read();

    }
}
  1. I wrote a exmaple to understand how shallow cloning works.In the above example in the
    clone method I am calling this.MemberWiseClone() , since in the class I have not implemented the memeberwiseclone . who will provide the default implementation?
  • 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-22T02:50:41+00:00Added an answer on May 22, 2026 at 2:50 am

    Memberwiseclone first create instance probably using Activator.CreateInstance which then iterates over all the fields within the type and set the value to the corresponding member/field.

    But I would rather not use ICloneable at all. If I needed to use Cloning I use BinaryFormatter to serialize an object graph then deserialize it so that I would get new deeply cloned instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class foo { public: void set(const int a) {b=a;} private: int b; }; Here
class Array { double *mx; int mn; public: Array(); ~Array(){delete []mx}; Array& operator-(Array& b);
class Connection { public: typedef boost::shared_ptr<Connection> pointer; static pointer create(boost::asio::io_service& io_service){return pointer(new Connection(io_service));} explicit
class foo { public void bar(int i) { ... }; public void bar(long i)
class A { private: B* my2DArray[max1][max2]; }; How to initialize my2DArray with NULL here?
class mydialog declaration. ........................... my work is to create a window with a button
Class Carro { String name String marca String matricula } Class CarroMovel{ String pro1
class Office < ActiveRecord::Base has_many :users searchable do text :name location :coordinates do Sunspot::Util::Coordinates.new(latitude,
class Person has_many :owned_groups, :class_name => Group, :foreign_key => :owner_id has_many :owned_group_memberships, :through =>
class Area { String name String description static constraints = { } _ class

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.