Type.Equals()方法用於檢查當前Type的基礎係統類型是否與指定的Object或Type的基礎係統類型相同。此方法的重載列表中有2種方法,如下所示:
- Equals(Type)方法
- Equals(Object)方法
Type.Equals(Type) Method
此方法用於檢查當前Type的基礎係統類型是否與指定Type的基礎係統類型相同。
用法: public virtual bool Equals (Type o);
Here, it takes the object whose underlying system type is to be compared with the underlying system type of the current Type.
返回值:如果o的基礎係統類型與當前Type的基礎係統類型相同,則此方法返回true,否則返回false。
以下示例程序旨在說明Type.Equals()方法的使用:
示例1:
// C# program to demonstrate the
// Type.Equals(Type) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
Type value1 = typeof(System.String);
// Declaring and initializing value2
Type value2 = typeof(System.Int32);
// using Equals(Type) method
bool status = value1.Equals(value2);
// checking the status
if (status)
Console.WriteLine("{0} is equal to {1}",
value1, value2);
else
Console.WriteLine("{0} is not equal to {1}",
value1, value2);
}
}
輸出:
System.String is not equal to System.Int32
示例2:
// C# program to demonstrate the
// Type.Equals(Type) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get(typeof(System.String), typeof(System.String));
get(typeof(System.String), typeof(System.Int32));
get(typeof(System.Decimal), typeof(System.Double));
}
// defining get() method
public static void get(Type value1,
Type value2)
{
// using Equals(Type) method
bool status = value1.Equals(value2);
// checking the status
if (status)
Console.WriteLine("{0} is equal to {1}",
value1, value2);
else
Console.WriteLine("{0} is not equal to {1}",
value1, value2);
}
}
輸出:
System.String is equal to System.String System.String is not equal to System.Int32 System.Decimal is not equal to System.Double
Type.Equals(Object) Method
此方法用於檢查當前定義的Type對象的基礎係統類型是否與指定Object的基礎係統類型完全相同。
用法: public override bool Equals (object obj);
Here, it takes the object whose underlying system type is to be compared with the underlying system type of the current Type. For the comparison to succeed, obj must be able to be cast or converted to an object of type Type.
返回值:如果obj的基礎係統類型與當前Type的基礎係統類型相同,則此方法返回true,否則返回false。如果obj為null或無法強製轉換或轉換為Type對象,則此方法還返回false。
以下示例程序旨在說明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.Equals(Object) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
Type value1 = typeof(int);
// Declaring and initializing value2
object value2 = typeof(int);
// using Equals(Object) method
bool status = value1.Equals(value2);
// checking the status
if (status)
Console.WriteLine("{0} is equal to {1}",
value1, value2);
else
Console.WriteLine("{0} is not equal to {1}",
value1, value2);
}
}
輸出:
System.Int32 is equal to System.Int32
示例2:
// C# program to demonstrate the
// Type.Equals(Object) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get(typeof(int), new Object());
get(typeof(System.String), (object)5.5);
get(typeof(System.String), null);
}
// defining get() method
public static void get(Type value1,
object value2)
{
// using Equals(Object) method
bool status = value1.Equals(value2);
// checking the status
if (status)
Console.WriteLine("{0} is equal to {1}",
value1, value2);
else
Console.WriteLine("{0} is not equal to {1}",
value1, value2);
}
}
輸出:
System.Int32 is not equal to System.Object System.String is not equal to 5.5 System.String is not equal to
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.Equals() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。