📜  C#| Char.Equals()方法

📅  最后修改于: 2021-05-29 17:30:59             🧑  作者: Mango

在C#中, Char.Equals()System.Char struct方法,该方法用于通过检查当前实例是否等于指定的对象或Char值来返回值。可以通过向其传递不同类型的参数来重载此方法。

  1. Char.Equals(Char)方法
  2. Char.Equals(Object)方法

Char.Equals(Char)方法

此方法用于通过检查当前实例是否等于指定的Char对象来返回值。

句法:

public bool Equals(Char ob);

范围:

返回类型:如果给定的ob参数等于当前实例的值,则返回true,否则返回false 。此方法的返回类型为System.Boolean

例子:

// C# program to illustrate the
// Char.Equals(Char) Method
using System;
  
public class GeeksforGeeks {
  
    // Main method
    public static void Main() {
  
        // declaration of datatype
        bool result;
        char ch1 = 'G';
  
        // checking if 'G' is equal or not
  
        // Here we are passing char G as the
        // parameter to the Equals Method
        result = ch1.Equals('G');
  
        Console.WriteLine(result);    
          
        // checking if 'v' is equal or not
        char ch2 = 'v';
  
        // Here we are passing char W as the
        // parameter to the Equals Method
        result = ch2.Equals('W');
  
        Console.WriteLine(result);        
    }
}
输出:
True
False

Char.Equals(Object)方法

此方法用于通过检查当前实例是否等于指定的对象来返回值。

句法:

public override bool Equals(object ob);

范围:

返回类型:如果给定的ob参数是Char的一个实例,并且等于当前实例的值,则它返回true,否则返回false 。此方法的返回类型为System.Boolean

例子:

// C# program to illustrate the
// Char.Equals(Object) Method
using System;
  
public class GeeksforGeeks {
  
    // Main method
    public static void Main() {
  
        // Declaration of data type
        bool result;
  
        // Checking if 'G' is equal or not
        char ch1 = 'G';
  
        // Here we are passing object ch1 as the
        // parameter to the Equals Method
        result = 'G'.Equals(ch1);
  
        Console.WriteLine(result);  
   
        // Checking if 'v' is equal or not
        char ch2 = 'v';
  
         // Here we are passing object ch2 as the
        // parameter to the Equals Method
        result = 'x'.Equals(ch2);
  
        Console.WriteLine(result);
    }
}
输出:
True
False

参考: https://docs.microsoft.com/zh-cn/dotnet/api/system.char.equals?view=netframework-4.7.2