c# object clone
C#
using System;
public class IdInfo
{
public int IdNumber;
public IdInfo(int IdNumber)
{
this.IdNumber = IdNumber;
}
}
public class Person
{
public int Age;
public string Name;
public IdInfo IdInfo;
public Person ShallowCopy()
{
return (Person) this.MemberwiseClone();
}
public Person DeepCopy()
{
Person other = (Person) this.MemberwiseClone();
other.IdInfo = new IdInfo(IdInfo.IdNumber);
other.Name = String.Copy(Name);
return other;
}
}
public class Example
{
public static void Main()
{
// Create an instance of Person and assign values to its fields.
Person p1 = new Person();
p1.Age = 42;
p1.Name = "Sam";
p1.IdInfo = new IdInfo(6565);
// Perform a shallow copy of p1 and assign it to p2.
Person p2 = p1.ShallowCopy();
// Display values of p1, p2
Console.WriteLine("Original values of p1 and p2:");
Console.WriteLine(" p1 instance values: ");
DisplayValues(p1);
Console.WriteLine(" p2 instance values:");
DisplayValues(p2);
// Change the value of p1 properties and display the values of p1 and p2.
p1.Age = 32;
p1.Name = "Frank";
p1.IdInfo.IdNumber = 7878;
Console.WriteLine("\nValues of p1 and p2 after changes to p1:");
Console.WriteLine(" p1 instance values: ");
DisplayValues(p1);
Console.WriteLine(" p2 instance values:");
DisplayValues(p2);
// Make a deep copy of p1 and assign it to p3.
Person p3 = p1.DeepCopy();
// Change the members of the p1 class to new values to show the deep copy.
p1.Name = "George";
p1.Age = 39;
p1.IdInfo.IdNumber = 8641;
Console.WriteLine("\nValues of p1 and p3 after changes to p1:");
Console.WriteLine(" p1 instance values: ");
DisplayValues(p1);
Console.WriteLine(" p3 instance values:");
DisplayValues(p3);
}
public static void DisplayValues(Person p)
{
Console.WriteLine(" Name: {0:s}, Age: {1:d}", p.Name, p.Age);
Console.WriteLine(" Value: {0:d}", p.IdInfo.IdNumber);
}
}
// The example displays the following output:
// Original values of p1 and p2:
// p1 instance values:
// Name: Sam, Age: 42
// Value: 6565
// p2 instance values:
// Name: Sam, Age: 42
// Value: 6565
//
// Values of p1 and p2 after changes to p1:
// p1 instance values:
// Name: Frank, Age: 32
// Value: 7878
// p2 instance values:
// Name: Sam, Age: 42
// Value: 7878
//
// Values of p1 and p3 after changes to p1:
// p1 instance values:
// Name: George, Age: 39
// Value: 8641
// p3 instance values:
// Name: Frank, Age: 32
// Value: 7878
Also in C#:
- Title
- mysqldump - date
- Category
- C#
- Title
- change canvas color uwp c#
- Category
- C#
- Title
- get what week of the month c#
- Category
- C#
- Title
- instantiate offset unity
- Category
- C#
- Title
- Replaced OS is obselete
- Category
- C#
- Title
- unity draw wire square
- Category
- C#
- Title
- generics in c#
- Category
- C#
- Title
- c# exit for
- Category
- C#
- Title
- c# mailmessage set sender name
- Category
- C#
- Title
- c# get set
- Category
- C#
- Title
- unity prevent system from creation
- Category
- C#
- Title
- .netstandard distinctby iqueryable
- Category
- C#
- Title
- csv to dataset c#
- Category
- C#
- Title
- iphone
- Category
- C#
- Title
- unity movetowards 2d
- Category
- C#
- Title
- c# how to open file explorer
- Category
- C#
- Title
- c# download string url
- Category
- C#
- Title
- lat long data type c#
- Category
- C#
- Title
- c# get date without time
- Category
- C#
- Title
- c# error "The name 'ViewBag' does not exist in the current context"
- Category
- C#
- Title
- unity rotate around point
- Category
- C#
- Title
- read embedded resource c# xml
- Category
- C#
- Title
- c# distinct array of objects by values
- Category
- C#
- Title
- leantween sequence
- Category
- C#
- Title
- unity mobile controls
- Category
- C#
- Title
- c# cancellationtoken
- Category
- C#
- Title
- c# switch case with or condition
- Category
- C#
- Title
- c# format number with leading zeros
- Category
- C#
- Title
- c# postfix increment operator overload
- Category
- C#
- Title
- dto and automapper
- Category
- C#
- Title
- vb.net yes no cancel
- Category
- C#
- Title
- unity particle system color
- Category
- C#
- Title
- access dic by key c#
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- unity change the source image or image
- Category
- C#
- Title
- winforms timer c#
- Category
- C#
- Title
- insert variables into string c#
- Category
- C#
- Title
- c# loop through list
- Category
- C#
- Title
- bold caption latex
- Category
- C#
- Title
- what is a protected int c#
- Category
- C#
- Title
- strtorime online
- Category
- C#
- Title
- onmouseover unity
- Category
- C#
- Title
- unity print to console
- Category
- C#
- Title
- unity to integer
- Category
- C#
- Title
- how to declare a string c#
- Category
- C#
- Title
- how to stop a form c#
- Category
- C#
- Title
- c# add to array
- Category
- C#
- Title
- edit bool from diffirent script
- Category
- C#
- Title
- asp.net core redirecttoaction with parameters
- Category
- C#
- Title
- drag object unity 2d
- Category
- C#
- Title
- unity line renderer position count
- Category
- C#
- Title
- blazor wasm routable page in separate project
- Category
- C#
- Title
- unity get number of child objects
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- c# convert column name to number
- Category
- C#
- Title
- button commandfield commandargument pass textbox
- Category
- C#
- Title
- arry in c#
- Category
- C#
- Title
- unity print
- Category
- C#
- Title
- c# convert Unix time in seconds to datetime
- Category
- C#
- Title
- What is a class in c#
- Category
- C#
- Title
- __dopostback
- Category
- C#
- Title
- unity simple fps controller
- Category
- C#
- Title
- how to get component in unity c#
- Category
- C#
- Title
- how to use more than one condition in ef join
- Category
- C#
- Title
- nullreferenceexception c#
- Category
- C#
- Title
- unity load next scene
- Category
- C#
- Title
- how to compare datetime in c#
- Category
- C#
- Title
- c# close window
- Category
- C#
- Title
- unity how to change the text on a button
- Category
- C#
- Title
- unity transfoprm position y change
- Category
- C#
- Title
- c# build string out of list of strings
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- how to add array to list in c#
- Category
- C#
- Title
- top down movement unity c#
- Category
- C#
- Title
- or in C#
- Category
- C#
- Title
- set object to random color unity
- Category
- C#
- Title
- math class C# exponents
- Category
- C#
- Title
- get number of sundays in a month c#
- Category
- C#
- Title
- c# main method
- Category
- C#
- Title
- selection sort c#
- Category
- C#
- Title
- change text color wpf
- Category
- C#
- Title
- how to say or in c#
- Category
- C#
- Title
- random from list c#
- Category
- C#
- Title
- get attribute value of xml element c#
- Category
- C#
- Title
- unity input get axis
- Category
- C#
- Title
- take screenshot in c#
- Category
- C#
- Title
- c# trigger destructor
- Category
- C#
- Title
- c# dynamic object get value
- Category
- C#
- Title
- print gridcontrol devexpress
- Category
- C#
- Title
- unity how to make jump script
- Category
- C#
- Title
- unity addforceatposition
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- unity accessing 2d pointlight from c# script
- Category
- C#
- Title
- javascript append
- Category
- C#
- Title
- how to change the title of the console in c#
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- convert string to int c#
- Category
- C#
- Title
- c# string array to string
- Category
- C#
- Title
- how to pass an optional parameter in c# mvc asp.net
- Category
- C#
- Title
- c# linq join multiple conditions
- Category
- C#