C# language: 10. Cloning objects

in programming •  7 years ago 

CC.png

Hi, today I will explain briefly what is the cloning of objects, let me just say it in a nutshell, today I will only say what is more or less the point. There are two types of types in C #, valuable and reference types. The former are usually the types int, float, char, double etc. and the latter are usually classes, interface, delegations, etc.

If we want to copy a value of value type eg int to another type of value, we do it in a very simple way:

int a = 8;
int b = a;
Console.Write(b);

This script will display the number 8 on the screen, because we assigned the variable “a” to the variable “b”

What is the copying of the values we already know, and what is the copying of references? In a nutshell, this is copying the address in the memory which our object are referenced.

Let’s assume that we made two cakes and we want to add more salt to these two cakes, we will do the whole thing in this way:

Cake cake1 = new Cake(double.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()),int.Parse(Console.ReadLine()));
 
Cake cake2 = new Cake(double.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()));
 
cake1.AmountOfSalt = double.Parse(Console.ReadLine());
 
cake2 = cake1;
Console.WriteLine(cake1.AmountOfSalt);
Console.WriteLine(cake2.AmountOfSalt);

First, we create cakes objects, after creating cakes objects, change the amount of salt poured in the first dough and clone these two objects and what we enter after creating the objects will add to both cakes and will display the same on the screen.

C # also has the Equals() method that we use when we want to check if two objects come from the same memory location. A small example:

Cake cake1 = new Cake(double.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()),int.Parse(Console.ReadLine()));
 
Cake cake2 = new Cake(double.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()));
 
cake1.AmountOfSalt = double.Parse(Console.ReadLine());
 
cake2 = cake1;
Console.WriteLine(cake1.AmountOfSalt);
Console.WriteLine(cake2.AmountOfSalt);
if (cake1.Equals(cake2))
     Console.WriteLine("The objects refer to the same address");
else
     Console.WriteLine("Objects do not refer to the same address");

This content also you can find on my blog http://devman.pl/csharplan/c-language-10-cloning-objects/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

That’s all on today, of course I have briefly said about cloning is still shallow and deep cloning, but we will say about that another time, this what I explained above is the most important, bye!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!