Câu hỏi phỏng vấn C#
Câu hỏi

What is the method Me...

Câu trả lời

The MemberwiseClone() method in C# creates a shallow copy of the current object. Here's what it does:

  1. It creates a new instance of the same type as the original object.
  2. It copies the values of all fields of the original object to the new object.
  3. If the original object has reference type fields, it copies the references to the same objects referenced by the original object's fields.

In other words, MemberwiseClone() creates a new object with the same field values as the original object, but it does not create new copies of the referenced objects. This means that if the original object has reference type fields, both the original object and the cloned object will reference the same objects.

Here's an example:

csharp Copy
class Person
{
    public string Name { get; set; }
    public Address Address { get; set; }
}

class Address
{
    public string Street { get; set; }
    public string City { get; set; }
}

static void Main(string[] args)
{
    Person person1 = new Person
    {
        Name = "John Doe",
        Address = new Ad...
expert

expert

Gợi ý câu hỏi phỏng vấn

expert

Could you explain the difference between Func vs. Action vs. Predicate ?

expert

What are the benefits of a Deferred Execution in LINQ?

senior

What is the difference between Func<string,string> and delegate ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào