Share or c# operator

Quá tải trong C#

#C ##operatoroverloading #Coding #Programming #tutorial

Quá tải toán tử là một tính năng trong C# cho phép bạn xác định ý nghĩa mới cho các toán tử hiện có.Điều này có thể hữu ích để làm cho mã của bạn dễ đọc và súc tích hơn, hoặc cung cấp hỗ trợ cho các loại dữ liệu tùy chỉnh.

Để quá tải một toán tử, bạn chỉ cần xác định một phương thức có cùng tên với toán tử và cùng số lượng tham số.Ví dụ: để quá tải toán tử bổ sung (+), bạn sẽ xác định một phương thức như sau:

`` `C#
công khai int add (int a, int b)
{
trả lại A + B;
}
`` `

Sau đó, bạn có thể sử dụng phương pháp này để thêm hai số nguyên lại với nhau, như thế này:

`` `C#
int x = 10;
int y = 20;
int z = x + y;// z hiện bằng 30
`` `

Bạn cũng có thể quá tải các toán tử cho các loại dữ liệu tùy chỉnh.Ví dụ: mã sau quá tải người vận hành bổ sung cho một lớp tùy chỉnh có tên là `person`:

`` `C#
người lớp công khai
{
Chuỗi công khai FirstName {get;bộ;}
chuỗi công khai lastName {get;bộ;}

người tĩnh công cộng thêm (người A, người B)
{
trả lại người mới
{
FirstName = a.firstname + b.firstname,
LastName = a.lastname + b.lastname
};
}
}
`` `

Bây giờ, bạn có thể thêm hai đối tượng `person` với nhau, như thế này:

`` `C#
Người p1 = người mới {firstName = "john", lastName = "doe"};
Người p2 = người mới {firstName = "jane", lastName = "doe"};
Người p3 = person.add (p1, p2);// P3 hiện là một đối tượng người mới với tên đầu tiên "Johnjane" và tên cuối cùng là "Doedoe"
`` `

Quá tải người vận hành có thể là một công cụ mạnh mẽ để làm cho mã của bạn dễ đọc và súc tích hơn.Nó cũng có thể được sử dụng để cung cấp hỗ trợ cho các loại dữ liệu tùy chỉnh, làm cho chúng hữu ích hơn trong một loạt các kịch bản.

## Người giới thiệu

* [C# Operading của nhà điều hành] (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overvoading)
=======================================
Overloading in C#

#C# #OperatorOverloading #Coding #Programming #tutorial

Operator overloading is a feature in C# that allows you to define new meanings for existing operators. This can be useful for making your code more readable and concise, or for providing support for custom data types.

To overload an operator, you simply need to define a method with the same name as the operator and the same number of parameters. For example, to overload the addition operator (+), you would define a method like this:

```c#
public static int Add(int a, int b)
{
return a + b;
}
```

You can then use this method to add two integers together, like this:

```c#
int x = 10;
int y = 20;
int z = x + y; // z is now equal to 30
```

You can also overload operators for custom data types. For example, the following code overloads the addition operator for a custom class called `Person`:

```c#
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }

public static Person Add(Person a, Person b)
{
return new Person
{
FirstName = a.FirstName + b.FirstName,
LastName = a.LastName + b.LastName
};
}
}
```

Now, you can add two `Person` objects together, like this:

```c#
Person p1 = new Person { FirstName = "John", LastName = "Doe" };
Person p2 = new Person { FirstName = "Jane", LastName = "Doe" };
Person p3 = Person.Add(p1, p2); // p3 is now a new Person object with the first name "JohnJane" and the last name "DoeDoe"
```

Operator overloading can be a powerful tool for making your code more readable and concise. It can also be used to provide support for custom data types, making them more useful in a wider variety of scenarios.

## References

* [C# Operator Overloading](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overloading)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top