[C#] Class assignment is a reference

In C#, class assignment is to assign an instance of one class to an instance of another class or to assign an instance of a class to a variable.

For example, suppose there is a class named Person:

```csharp
public class Person
{ public string Name { get; set; } public int Age { get; set; } } ``` Then two Person objects can be created, and Assign one object to another: ```csharp Person person1 = new Person(); person1.Name = "John"; person1.Age = 25; Person person2 = new Person(); person2 = person1; // will person1 is assigned to person2 ``` In this example, both person1 and person2 are instances of the Person class. By assigning person1 to person2, person2 will reference the same object as person1. This means that changes to person2 will also affect person1. You can also assign an instance of a class to a variable: ```csharp Person person = new Person(); person.Name = "John"; person.Age = 25;

























string name = person.Name; //Assign the Name property of the person to the name variable
int age = person.Age; //Assign the Age property of the person to the age variable```In
this

example, assign the Name property of the person To the name variable, assign the person's Age attribute to the age variable. This way you can use these variables in subsequent code.

Guess you like

Origin blog.csdn.net/andeyeluguo/article/details/131891346