Share equals c#

ducbinh369

New member
## bằng trong c#

Toán tử bằng trong C# được sử dụng để so sánh hai đối tượng để xem chúng có giống nhau không.Cú pháp cho toán tử bằng là:

`` `
obj1 == obj2
`` `

Trong đó `obj1` và` obj2` là hai đối tượng được so sánh.

Toán tử bằng trả về giá trị boolean của `true` nếu hai đối tượng giống nhau và 'false' nếu chúng không.

Hai đối tượng được coi là giống nhau nếu chúng có cùng giá trị và cùng loại.Ví dụ, hai chuỗi sau được coi là giống nhau:

`` `
String1 = "Hello World"
String2 = "Hello World"
`` `

Hai số nguyên sau đây cũng được coi là giống nhau:

`` `
int1 = 10
int2 = 10
`` `

Tuy nhiên, hai đối tượng sau đây không được coi là giống nhau:

`` `
String1 = "Hello World"
int1 = 10
`` `

Điều này là do `String1` và` int1` có các loại khác nhau.

Toán tử bằng cũng có thể được sử dụng để so sánh các đối tượng thực hiện giao diện `iequalitycomparer`.Giao diện `iequalitycomparer` định nghĩa một phương thức gọi là` bằng` có hai đối tượng cùng loại với các tham số của nó và trả về giá trị boolean của `true` nếu các đối tượng giống nhau và 'false' nếu chúng không.

Ví dụ: mã sau sử dụng giao diện `iequalitycomparer` để so sánh hai chuỗi:

`` `
String1 = "Hello World"
String2 = "Hello World"

IequalityComparer <String> so sánh = new StringComparer ();

bool isequal = so sánh.equals (String1, String2);
`` `

Lớp `StringComparer` thực hiện giao diện` iequalitycomparer`, do đó nó có thể được sử dụng để so sánh các chuỗi.

## hashtags

* #C#
* #Equals
* #Operators
* #Comparison
* #các đối tượng
=======================================
## Equals in C#

The equals operator in C# is used to compare two objects to see if they are the same. The syntax for the equals operator is:

```
obj1 == obj2
```

Where `obj1` and `obj2` are the two objects to be compared.

The equals operator returns a boolean value of `true` if the two objects are the same, and `false` if they are not.

Two objects are considered to be the same if they have the same value and the same type. For example, the following two strings are considered to be the same:

```
string1 = "Hello world"
string2 = "Hello world"
```

The following two integers are also considered to be the same:

```
int1 = 10
int2 = 10
```

However, the following two objects are not considered to be the same:

```
string1 = "Hello world"
int1 = 10
```

This is because `string1` and `int1` are of different types.

The equals operator can also be used to compare objects that implement the `IEqualityComparer` interface. The `IEqualityComparer` interface defines a method called `Equals` that takes two objects of the same type as its parameters and returns a boolean value of `true` if the objects are the same, and `false` if they are not.

For example, the following code uses the `IEqualityComparer` interface to compare two strings:

```
string1 = "Hello world"
string2 = "Hello world"

IEqualityComparer<string> comparer = new StringComparer();

bool areEqual = comparer.Equals(string1, string2);
```

The `StringComparer` class implements the `IEqualityComparer` interface, so it can be used to compare strings.

## Hashtags

* #C#
* #Equals
* #Operators
* #Comparison
* #Objects
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top