1

I'm making a generic method in which the Type of T is gonna be int, long, float, or double. I've searched and came up with this solution and now want to exclude DateTime from the generic constraints.

What I want is something like:

class SomeGeneric<T> where T : unmanaged, IComparable, IEquatable<T> not System.Runtime.Serialization.ISerializable
{
//...
}

As DateTime is inheriting from all the interfaces that my numeric types are inheriting but DateTime is inheriting from ISerializable as well so I want to exclude ISerializable.

Hossein Ebrahimi
  • 632
  • 10
  • 20
  • 1
    This sounds like an XY problem. What problem are you trying to solve? – Sweeper Aug 03 '20 at 08:15
  • I want the generic parameter to not accept DateTime – Hossein Ebrahimi Aug 03 '20 at 08:17
  • I know that, but why would that be useful? What is your _ultimate goal_ here? – Sweeper Aug 03 '20 at 08:18
  • 4
    This is not how generics work. Its also not really designed to limit to certain primative types. If you find yourself wanting to constrain to a narrow range of numeric types, you are more than likely **not** wanting to use generics, and would seemingly want regular overloads. – TheGeneral Aug 03 '20 at 08:19
  • Actually not possible. https://stackoverflow.com/questions/10642751/excluding-types-in-the-generic-constraints-possible –  Aug 03 '20 at 08:23
  • That's right. It is a kind of interview question. I don't know what is the goal of asking these questions. – Hossein Ebrahimi Aug 03 '20 at 08:23
  • What is the actual text of the interview question? Perhaps it's getting at something else. – Matthew Watson Aug 03 '20 at 08:36
  • @MatthewWatson Write a method that gets a list of a type and a variable of that type, and returns the list with members that are greater than the given variable. The type must be int, long, float or double. – Hossein Ebrahimi Aug 03 '20 at 08:41
  • Are you sure you're paraphrasing that interview question correctly? Because I have some questions about it. For instance, "list of a type", you mean list of "int"? OK, but then "with members that are greater than the given variable", members of what? of an int? – Lasse V. Karlsen Aug 03 '20 at 08:45
  • 1
    There is no way to specify, using generics, that only int, long, double and float are legal. You could create 4 overloads to the method, or 4 differently named methods, but using generic `where` clause, it is not possible. The 5 types (including DateTime) implement the same basic interfaces so there is no way to get rid of DateTime. DateTime does implement ISerializable as well, as you observed, but you cannot create a negative generic `where` clause. The other types does not implement any interface DateTime does not, so you can't even filter using a positive `where` clause. – Lasse V. Karlsen Aug 03 '20 at 08:51
  • Until here I think I should throw an exception if the parameter was of type DateTime. In addition to the constraint. which is not interesting but the only way I could find – Hossein Ebrahimi Aug 03 '20 at 08:55
  • 1
    If the question really does mean that it should NOT support anything other than the named types, you should reject *any* type that isn't one of them - not just DateTime. Otherwise someone could write their own type that fulfils all the generic constraints. – Matthew Watson Aug 03 '20 at 09:04

0 Answers0