>  기사  >  백엔드 개발  >  C# 개체가 같음

C# 개체가 같음

WBOY
WBOY원래의
2024-09-03 15:06:02200검색

현재 인스턴스가 주어진 개체와 같은지 비교하려면 C#에서 Equals(Object) 함수를 사용합니다. 이 함수는 현재 인스턴스와 비교할 개체를 매개 변수로 사용하고 매개변수인 현재 인스턴스와 비교되는 객체가 현재 인스턴스와 동일하면 true를 반환하고, 매개변수인 현재 인스턴스와 비교되는 객체가 현재 인스턴스와 동일하지 않은 경우 이 함수는 false를 반환합니다. .NET 5.0 버전 및 기타 35개 버전에서 사용할 수 있습니다.

Equals() 함수 선언 구문:

Equals(Object);

객체는 현재 인스턴스와 비교되는 객체입니다.

Equals(Object) 함수의 작동

  • 현재 인스턴스가 주어진 개체와 같은지 비교하려면 C#에서 Equals(Object) 함수라는 함수를 사용합니다.
  • Equals(Object) 함수는 현재 인스턴스와 비교할 개체를 매개변수로 사용합니다.
  • Equals(Object) 함수는 매개변수로 현재 인스턴스와 비교할 객체가 현재 인스턴스와 동일한 경우 true를 반환합니다.
  • Equals(Object) 함수는 매개변수로 현재 인스턴스와 비교할 객체가 현재 인스턴스와 동일하지 않은 경우 false를 반환합니다.
  • Equals(Object) 기능은 .NET 5.0 버전과 기타 35개 버전에서 사용할 수 있습니다.

C# 개체 같음의 예

다음은 C# Object Equals의 예입니다.

예시 #1

객체를 생성하고 이를 Equals(Object) 함수에 매개 변수로 전달하여 현재 인스턴스와 비교하고 결과를 표시하는 C# 프로그램입니다.

코드:

using System.IO;
using System;
public class check
{
public static void Main()
{
//declaring a Boolean variable called firstvalue and storing a Boolean value in it
bool firstvalue = false;
//declaring an object called secondvalue and storing an expression in it
object secondvalue = 1 * 0;
//using Equals(object) method to compare the firstvalue with the current instance secondvalue and storing the result in a variable called result
bool result = firstvalue.Equals(secondvalue);
//based on the result, displaying if the firstvalue is equal to the secondvalue or not
if (result)
{
Console.WriteLine("The first value {0} is equal to the second value {1}", firstvalue, secondvalue);
}
else
{
Console.WriteLine("The first value {0} is not equal to the second value {1}", firstvalue, secondvalue);
}
}
}

출력:

C# 개체가 같음

위 프로그램에서는 firstvalue라는 부울 변수를 정의하고 여기에 부울 값을 저장합니다. 그런 다음 secondvalue라는 개체를 정의하고 그 안에 표현식을 저장합니다. 그런 다음 Equals(Object) 함수를 사용하여 첫 번째 값 변수에 저장된 값과 두 번째 값 개체에 저장된 값을 비교한 다음 그 결과를 결과 변수라는 변수에 저장합니다. 그런 다음 if 조건문을 사용하여 얻은 결과에 따라 첫 번째 값이 두 번째 값과 같은지 여부를 표시합니다.

예시 #2

객체를 생성하고 이를 Equals(Object) 함수에 매개 변수로 전달하여 현재 인스턴스와 비교하고 결과를 표시하는 C# 프로그램입니다.

코드:

using System.IO;
using System;
public class check
{
public static void Main()
{
//declaring a Boolean variable called firstvalue and storing a Boolean value in it
bool firstvalue = true;
//declaring an object called secondvalue and storing an expression in it
object secondvalue = 10 /10;
//using Equals(object) method to compare the firstvalue with the current instance secondvalue and storing the result in a variable called result
bool result = firstvalue.Equals(secondvalue);
//based on the result, displaying if the firstvalue is equal to the secondvalue or not
if (result)
{
Console.WriteLine("The first value {0} is equal to the second value {1}", firstvalue, secondvalue);
}
else
{
Console.WriteLine("The first value {0} is not equal to the second value {1}", firstvalue, secondvalue);
}
}
}

출력:

C# 개체가 같음

위 프로그램에서는 firstvalue라는 부울 변수를 정의하고 여기에 부울 값을 저장합니다. 그런 다음 secondvalue라는 개체를 정의하고 그 안에 표현식을 저장합니다. 그런 다음 Equals(Object) 함수를 사용하여 첫 번째 값 변수에 저장된 값과 두 번째 값 개체에 저장된 값을 비교한 다음 그 결과를 결과 변수라는 변수에 저장합니다. 그런 다음 if 조건문을 사용하여 얻은 결과에 따라 첫 번째 값이 두 번째 값과 같은지 여부를 표시합니다.

예시 #3

객체를 생성하고 이를 Equals(Object) 함수에 매개 변수로 전달하여 현재 인스턴스와 비교하고 결과를 표시하는 C# 프로그램입니다.

코드:

using System.IO;
using System;
public class check
{
public static void Main()
{
//declaring a Boolean variable called firstvalue and storing a Boolean value in it
bool firstvalue = true;
//declaring an object called secondvalue and storing an expression in it
object secondvalue = 10 - 5;
//using Equals(object) method to compare the firstvalue with the current instance secondvalue and storing the result in a variable called result
bool result = firstvalue.Equals(secondvalue);
//based on the result, displaying if the firstvalue is equal to the secondvalue or not
if (result)
{
Console.WriteLine("The first value {0} is equal to the second value {1}", firstvalue, secondvalue);
}
else
{
Console.WriteLine("The first value {0} is not equal to the second value {1}", firstvalue, secondvalue);
}
}
}

출력:

C# 개체가 같음

위 프로그램에서는 firstvalue라는 부울 변수를 정의하고 여기에 부울 값을 저장합니다. 그런 다음 secondvalue라는 개체를 정의하고 그 안에 표현식을 저장합니다. 그런 다음 Equals(Object) 함수를 사용하여 첫 번째 값 변수에 저장된 값과 두 번째 값 개체에 저장된 값을 비교한 다음 그 결과를 결과 변수라는 변수에 저장합니다. 그런 다음 if 조건문을 사용하여 얻은 결과에 따라 첫 번째 값이 두 번째 값과 같은지 여부를 표시합니다.

위 내용은 C# 개체가 같음의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 개체 직렬화다음 기사:C# 개체 직렬화