Programmer2120 Programmer2120 - 1 year ago 157
C# Question

How to Set Two Instances of a Class Equal to Each Other by VALUE c#

I want to be able to set one instance of a class equal another BY VALUE. I.E. I don't want to change a property in one instance and have the change appear in the other. And it CAN'T BE A STRUCT

Given theses classes:

class Main(string args[]) {
Dictionary<string, FT> dic = new Dictionary<string, FT>();
FT ft = new FT();
ft.Name = "1";
dic.Add("a", ft);
ft.Name = "2";


class FT {
public string Name { get; set; }

If you run this it will (should) printout '2'. That is not what I need. I need to still print '1'. And it CAN'T be a struct



I can't just do:

var x = new FT();
x.Name = this.Name;

Because I will have easily 100 properties and variables in the end. And doing:

x.propertyName = this.propertyName;

100 times, is VERY inefficient.

Answer Source


ft= new FT ();

Before changing value to 2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download