Skip to content

Instantly share code, notes, and snippets.

@ukcoderj
Created June 14, 2019 10:31
Show Gist options
  • Save ukcoderj/03d6b60155dcf39e42e5cd28a204ac1c to your computer and use it in GitHub Desktop.
Save ukcoderj/03d6b60155dcf39e42e5cd28a204ac1c to your computer and use it in GitHub Desktop.
Copy Constructor Example
using System;
using System.Collections.Generic;
namespace DeleteMeConsole
{
public class StateChecking
{
public void DoTest()
{
Stuff stuff = new Stuff();
stuff.Message = "Hello world";
stuff.SubStuffs = new List<SubStuff>()
{
new SubStuff() { Name = "Ted", Age = 21 },
new SubStuff() { Name = "Fred", Age = 22 }
};
Console.WriteLine(stuff.Message);
Console.WriteLine(stuff.SubStuffs.Count);
Console.WriteLine(stuff.SubStuffs[0].Name);
SafeChange0(stuff);
Console.WriteLine("----Safe Result ------------");
Console.WriteLine(stuff.Message);
Console.WriteLine(stuff.SubStuffs.Count);
Console.WriteLine(stuff.SubStuffs[0].Name);
NotSafeChange1(stuff);
Console.WriteLine("--- Not safe 1-------------");
Console.WriteLine(stuff.Message);
Console.WriteLine(stuff.SubStuffs.Count);
Console.WriteLine(stuff.SubStuffs[0].Name);
AlterStuff2(stuff);
Console.WriteLine("---Not safe 2-------------");
Console.WriteLine(stuff.Message);
Console.WriteLine(stuff.SubStuffs.Count);
Console.WriteLine(stuff.SubStuffs[0].Name);
AlterStuff3(stuff);
Console.WriteLine("---Not safe 3-------------");
Console.WriteLine(stuff.Message);
Console.WriteLine(stuff.SubStuffs.Count);
Console.WriteLine(stuff.SubStuffs[0].Name);
AlterStuff4(ref stuff);
Console.WriteLine("---Not safe 4-------------");
Console.WriteLine(stuff.Message);
Console.WriteLine(stuff.SubStuffs.Count);
Console.WriteLine(stuff.SubStuffs[0].Name);
}
private void SafeChange0(Stuff st)
{
Stuff st1 = new Stuff(st); // USE COPY CONSTRUCTOR!!!!!!!
st1.Message = "This wont change";
st1.SubStuffs[0].Name = "My name wont change here";
st1.SubStuffs.Add(new SubStuff() { Name = "Another change that wont affect orig object", Age = 2 });
}
private void NotSafeChange1(Stuff st)
{
var st1 = st;
st1.Message = "NOT Safe change";
st1.SubStuffs[0].Name = "Mr NOT Safe";
st1.SubStuffs.Add(new SubStuff() { Name = "Omboblio", Age = 2 });
}
private void AlterStuff2(Stuff st)
{
st.Message = "Change 1";
st.SubStuffs[0].Name = "AlterStuff1";
st.SubStuffs.Add(new SubStuff() { Name = "Red", Age = 23 });
}
private static void AlterStuff3(Stuff st)
{
st.Message = "Change 2";
st.SubStuffs[0].Name = "static....";
st.SubStuffs.Add(new SubStuff() { Name = "staticboy", Age = 24 });
}
private void AlterStuff4(ref Stuff st)
{
st.Message = "Change 3";
st.SubStuffs[0].Name = "Ref";
st.SubStuffs.Add(new SubStuff() { Name = "reffy", Age = 25 });
}
}
public class Stuff
{
public Stuff()
{
}
public Stuff(Stuff stuff)
{
this.Message = stuff.Message;
this.SubStuffs = new List<SubStuff>();
// Lists MUST have copy constructors too!
foreach(var val in stuff.SubStuffs)
{
var newSubStuff = new SubStuff(val);
this.SubStuffs.Add(newSubStuff);
}
}
public string Message { get; set; }
public List<SubStuff> SubStuffs { get; set; }
}
public class SubStuff
{
public SubStuff()
{
}
public SubStuff(SubStuff subStuff)
{
this.Age = subStuff.Age;
this.Name = subStuff.Name;
}
public int Age { get; set; }
public string Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment