.NET API Reference

System Namespace

IQuatable<T> Interface

Namespace: System

Defines a method that a value type or class implements to provide a way to determine whether two instances are equal. This interface is generic and can be implemented for any type. This specific page refers to the specialization for string.

Syntax


public interface IQuatable<T>

Methods


bool Equals(T other);

Equals(T other)

Indicates whether the current object is equal to another object of the same type.

Parameters:

Returns: true if the current object is equal to the other parameter; otherwise, false.

Remarks

The IQuatable<T> interface is used to implement value-based equality checks. When you implement this interface, you are providing a custom logic for comparing two instances of your type. For the IQuatable<string> specialization, this comparison is typically a culture-sensitive or culture-invariant comparison of the string content.

By default, the string type in .NET implements its own equality checks, often using methods like string.Equals(). The generic IQuatable<string> contract ensures that this capability is available in a standardized way.

Example

While direct implementation of IQuatable<string> is not typical as strings already provide robust equality, here's a conceptual example of how it might be used in a generic context:


using System;

public class StringComparer
{
    public static bool CompareStrings<T>(T str1, T str2) where T : IEquatable<T>
    {
        if (str1 == null && str2 == null)
            return true;
        if (str1 == null || str2 == null)
            return false;

        return str1.Equals(str2);
    }

    public static void Main(string[] args)
    {
        string s1 = "Hello";
        string s2 = "hello";
        string s3 = "World";

        Console.WriteLine($"'{s1}' == '{s2}'? {CompareStrings(s1, s2)}"); // Output: 'Hello' == 'hello'? False (case-sensitive by default for string)
        Console.WriteLine($"'{s1}' == '{s3}'? {CompareStrings(s1, s3)}"); // Output: 'Hello' == 'World'? False
    }
}