|
- c# - How to compare values of generic types? - Stack Overflow
@gstercken: One problem with IComparable overloading the comparison operators is that there are situations where X Equals(Y) should return false, but X CompareTo(Y) should return zero (suggesting neither element is larger than the other) [e g an ExpenseItem may have a natural ordering with respect to TotalCost, and there may be no natural ordering for expense items whose cost is the same, but
- How do I get the type name of a generic type argument?
This extension method outputs the simple type name for non-generic types, and appends the list of generic arguments for generic types This works fine for scenarios where you don't need to worry about inner generic arguments, like IDictionary<int, IDictionary<int, string>>
- Creating a generic method in C# - Stack Overflow
I am trying to combine a bunch of similar methods into a generic method I have several methods that return the value of a querystring, or null if that querystring does not exist or is not in the correct format This would be easy enough if all the types were natively nullable, but I have to use the nullable generic type for integers and dates
- C# Generics and Type Checking - Stack Overflow
In case you happen to have a generic method that returns a generic value but doesn't have generic parameters, you can use default(T) + (T)(object) cast, together with C# 8 pattern matching type checks (as indicated in the other recent answers) Example:
- Using a using alias = class with generic types? [duplicate]
using LookupDictionary = System Collections Generic Dictionary<string, int>; Now I want to accomplish the same with a generic type, while preserving it as a generic type: using List<T> = System Collections Generic List<T>; But that doesn't compile, so is there any way to achieve creating this alias while leaving the type as generic?
- Java: Instanceof and Generics - Stack Overflow
Two options for runtime type checking with generics: Option 1 - Corrupt your constructor Let's assume you are overriding indexOf( ), and you want to check the type just for performance, to save yourself iterating the entire collection
- Using List lt;T gt; in C# (Generics) - Stack Overflow
C# will infer the type based on arguments, so explicitly specifying <MyClass1> isn't needed in this case - obviously if there are no parameters on the method, you will need to specify the generic arguments - but it's nice when they are inferred! –
- Nullable type as a generic parameter possible? - Stack Overflow
Multiple generic constraints can't be combined in an OR fashion (less restrictive), only in an AND fashion (more restrictive) Meaning that one method can't handle both scenarios The generic constraints also cannot be used to make a unique signature for the method, so you'd have to use 2 separate method names
|
|
|