C# Is it possible to override the comparator < in C#?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Comparator
Click For Summary
In C#, it is not possible to override the less-than (<) operator, as the language does not support virtual operators. The discussion highlights that C# types like Int16, Int32, and Int64 do not have virtual operators defined, while floating-point types do have public static operators, which cannot be overridden. The concept of achieving an O(1) sorting algorithm is questioned, as sorting inherently requires examining each element at least once. The idea of redefining the < operator to sort an array without accessing its elements is met with skepticism, as it implies reliance on compiler behavior rather than a valid sorting mechanism. The conversation concludes with doubts about the original poster's intentions, suggesting that the discussion may not be serious.
SlurrerOfSpeech
Messages
141
Reaction score
11
Is it possible to override the < operator in C#? Note: I'm saying override, not overload.

I ask because, if there is, then I think I have discovered an O(1) sorting algorithm.
 
Technology news on Phys.org
SlurrerOfSpeech said:
Is it possible to override the < operator in C#? Note: I'm saying override, not overload.

I ask because, if there is, then I think I have discovered an O(1) sorting algorithm.
I'm not certain, but I don't think it is possible.According to the C# docs here, the base class would need to have a virtual < operator that you would override in your derived class. If you are comparing ordinary number values, the C# types for Int16, Int32, and Int64 don't have any operators defined on these structs. The Single (32-bit floating point) and Double (64-bit floating point) types do have operators defined for <, >, and so on, but these are public static operators, not virtual.
 
I am a bit puzzled about how you can image a sorting algorithm in general (that is, not only in a narrow special case, like if we know all elements are already sorted) can perform as O(1) considering it has to read each of the N element at least once. I am also puzzled why you think the existence of such an algorithm should relate to whether or not it is possible to override the less-than operator in C#. Can you perhaps explain what you mean in more detail?
 
Filip Larsen said:
I am a bit puzzled about how you can image a sorting algorithm in general (that is, not only in a narrow special case, like if we know all elements are already sorted) can perform as O(1) considering it has to read each of the N element at least once.

If I can redefine the meaning of < then I can ensure that an array is sorted without looking at any of its elements
 
SlurrerOfSpeech said:
If I can redefine the meaning of < then I can ensure that an array is sorted without looking at any of its elements
Maybe your code doesn't have to look at the elements of the array. But then you rely on some feature of the compiler with the overridden < to sort the array. And that will not be O(1).
 
  • Like
Likes FactChecker
SlurrerOfSpeech said:
If I can redefine the meaning of < then I can ensure that an array is sorted without looking at any of its elements

Can you please explain how you plan to sort an array of elements without looking at the elements themselves? And by "sort" I am referring to its usual meaning, like explained at [1].

[1] https://en.wikipedia.org/wiki/Sorting_algorithm
 
Folks, I am afraid the OP is trolling us. (He's admitted this in another thread)
 
Nothing more to say here. Thread closed.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
1K
Replies
86
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K