Является ли строка - значение или тип ссылки в контексте мелкого копирования? [закрыто]C#

Место общения программистов C#
Ответить
Anonymous
 Является ли строка - значение или тип ссылки в контексте мелкого копирования? [закрыто]

Сообщение Anonymous »

Я читал статью о неглубокой VS Deep Copy в C#, и хотя я понял основную концепцию, одна часть примера мелкого копирования смутила меня. Код из статьи: < /p> класс:

Код: Выделить всё

public class Address
{
public string address { get; set; }
}
< /code>
Employee
класс:

Код: Выделить всё

public class Employee
{
//Value type Property
public string Name { get; set; }

//Value type Property
public string Department { get; set; }

//Reference type Property
public Address EmpAddress { get; set; }

//Creating a Cloned Object of the Current Object
public Employee GetClone()
{
//Both Cloned and Existing Object Point to the Same Memory Location of the Address Object
return (Employee)this.MemberwiseClone();
}
}
< /code>
And the Employee class is used in the Main
Метод программы клас:
class Program
{
static unsafe void Main(string[] args)
{
//Example to Understand Shallow Copy
//Creating Employee Object
Employee emp1 = new Employee
{
Name = "Anurag",
Department = "IT",
EmpAddress = new Address() { address = "BBSR" }
};

//Creating a Clone Object from the Existing Object
Employee emp2 = emp1.GetClone();

//Changing Name Property of Clone Object Will Not Reflect the Existing Object
emp2.Name = "Pranaya";

//Changing Address Property of Clone Object Will Reflect the Changes of the Existing Object
//This is because address is a reference type property and in the case of Shallow Copy
//Both Clone and Existing Object will point to the Same Memory Address
emp2.EmpAddress.address = "Mumbai";

Console.WriteLine("Emplpyee 1: ");
Console.WriteLine("Name: " + emp1.Name + ", Address: " + emp1.EmpAddress.address + ", Dept: " + emp1.Department);
Console.WriteLine("Emplpyee 2: ");
Console.WriteLine("Name: " + emp2.Name + ", Address: " + emp2.EmpAddress.address + ", Dept: " + emp2.Department);

Console.Read();
}
}
< /code>
If someone could explain how this interpretation makes sense, I would really appreciate it.

Подробнее здесь: https://stackoverflow.com/questions/796 ... py-context
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»