Reputation: 789
Is there a variable type (something like string
, int
, or bool
) for comparators? (==
, !=
, <=
, >=
, >
, <
) And if not, how does C# use them?
I can use
int integerVariable = 0;
but is there something I can use to do
comp comparatorVariable = ==;
?
Upvotes: 2
Views: 376
Reputation: 633
C# provides a generic interface called IComparable<T>
.
This interface has a single method: int CompareTo<T>
public interface IComparable<in T>
{
int CompareTo(T other);
}
To implement the comparative operators (==, !=, <=, >=, >, <), you need to implement these operators on the class that inherits IComparable
.
The date time "==" operator probably looks something like the following.
public static bool operator ==(DateTime d1, DateTime d2)
{
return d1.CompareTo(d2) == 0;
}
Upvotes: -1
Reputation: 203840
The various operators you've listed do not have a type no. Objects have types. You've listed operators, which are themselves not objects, and so have no type. If you're interested in how the C# language uses operators, you can look at the language spec; section 7.3 of the C# 5.0 specs is titled "Operators" and is filled with information on how C# handles operators.
Upvotes: 5