Single.Equals()方法用于获取一个值,该值指示Single的两个实例是否表示相同的值。此方法的重载列表中有2种方法,如下所示:
- 等于(单)方法
- 等于(对象)方法
Single.Equals(Single)方法
此方法用于返回一个值,该值指示此实例和指定的Single对象是否表示相同的值。
Syntax: public bool Equals (float obj);
Here, it takes a Single object to compare to this instance.
Return Value: This method returns true if obj is equal to this instance otherwise, false.
下面的程序说明了Single.Equals()方法的用法:
范例1:
// C# program to demonstrate the
// Single.Equals(Single) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
float value1 = 10.5f;
// Declaring and initializing value2
float value2 = 20.6f;
// using Equals(Single) 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);
}
}
输出:
10.5 is not equal to 20.6
范例2:
// C# program to demonstrate the
// Single.Equals(Single) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get(5f, 5f);
get(5.5f, 4.5f);
get(10f, 10f);
get(7.5f, 19.5f);
}
// defining get() method
public static void get(float value1,
float value2)
{
// using Equals(Single) 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);
}
}
输出:
5 is equal to 5
5.5 is not equal to 4.5
10 is equal to 10
7.5 is not equal to 19.5
Single.Equals(Object)方法
此方法用于返回一个值,该值指示此实例是否等于指定的对象。
Syntax: public override bool Equals (object obj);
Here, it takes an object to compare with this instance.
Return Value: This method returns true if obj is an instance of Single and equals the value of this instance otherwise, false.
下面的程序说明了上述方法的用法:
范例1:
// C# program to demonstrate the
// Single.Equals(Single) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
float value1 = 10.5f;
// Declaring and initializing value2
object value2 = 3 / 36;
// 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);
}
}
输出:
10.5 is not equal to 0
范例2:
// C# program to demonstrate the
// Single.Equals(object) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get(5f, new Object());
get(5.5f, (float)5.5);
get(10f, 10);
get(7.5f, 2 / 5);
}
// defining get() method
public static void get(float value1,
object value2)
{
// compare both float value
// 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);
}
}
输出:
5 is not equal to System.Object
5.5 is equal to 5.5
10 is not equal to 10
7.5 is not equal to 0
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.single.equals?view=netstandard-2.1