当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Single.Equals()用法及代码示例


Single.Equals()方法用于获取一个值,该值指示Single的两个实例是否表示相同的值。此方法的重载列表中有2种方法,如下所示:

    • 等于(单)方法
    • 等于(对象)方法

Single.Equals(Single) Method

此方法用于返回一个值,该值指示此实例和指定的Single对象是否表示相同的值。


用法: public bool Equals (float obj);
Here, it takes a Single object to compare to this instance.

返回值:如果obj等于此实例,则此方法返回true,否则返回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) Method

此方法用于返回一个值,该值指示此实例是否等于指定的对象。

用法: public override bool Equals (object obj);
Here, it takes an object to compare with this instance.

返回值:如果obj是Single的一个实例,则此方法返回true,否则等于此实例的值,否则返回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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Single.Equals() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。