Why generic classes in .Net are invariant towards theirs generic args? [duplicate] - c#

This question already has an answer here:
Why do I need an Interface for Covariance (out Type)?
(1 answer)
Closed 8 years ago.
Why in .Net templating a generic class is an invariant operation towards generic arguments?
Interfaces and delegates are not, but classes are.
For instance, I would like to be able to assign object of type Expression<Func<string>> to Expression<Func<object>>. As T in Func<T> is "out" and Expression is immutable, it would be reasonable to assign it as I have showed, right?

Had classes allowed variant type parameters, you wouldn't be able to use them in any field, since fields are always (at least sometimes) writable and readable.
That would limit the utility enough to make it not worth it.

Related

Passing <T> from a string parameter to use AddSingleton [duplicate]

This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 2 years ago.
Why can't I do this?
services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();
OR is there a better way of passing from a string
The method also has an overload to pass a type as parameter. So you can do:
services.AddSingleton(typeof(ShoppingCartCache));
Same also works with your example (although more prone to runtime errors):
services.AddSingleton(Type.GetType("ShoppingCartCache",true));
The reason it doesn't work is because generic types must be static, thus known at compilation.

Getting a type representation of a partially open generic type [duplicate]

This question already has answers here:
C# Language: How to get type of bound but open Generic class?
(4 answers)
Closed 6 years ago.
I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.
What I have tried:
typeof(IDictionary<string,>)
I get: Partially opened type is not permitted in 'typeof' expression compile error.
typeof(IDictionary<,>).MakeGenericType(typeof(string))
I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)
Why I want to do this:
I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.
Maybe something like that could answer to your problem:
class CustomDictionary<TValue> : Dictionary<string, TValue>
{
}
And this should work:
typeof(CustomDictionary<>).MakeGenericType(typeof(string))

Is there any advantage to declaring a class static in C#? [duplicate]

This question already has answers here:
When to use static classes in C# [duplicate]
(11 answers)
Closed 8 years ago.
In C#, you can declare a class as static, which requires all members to also be static. Is there any advantage (e.g. performance) to be gained by doing so? Or is it only a matter of making sure you don't accidentally declare instance members in your class?
Or is it only a matter of making sure you don't accidentally declare instance members in your class?
It's that, but it's more than that:
It prevents instantiation by not having an instance constructor at all (whereas all other classes have a constructor - either one you declare or an implicit parameterless one)
It expresses your intention clearly
Nothing can derive from your class (it's both sealed and abstract)
Nothing can declare a field of your class type
Nothing can use your type as a generic type argument
Basically it tells the compiler and other developers "This is never meant to be instantiated - so if it looks like you're trying to use an instance, you're doing it wrong."
I doubt that there are any performance benefits, but all the above are enough for me :)
Oh, and you can only declare extension methods in static classes too...
I guess you are close to answer, sharing the link which might answer your question.
https://softwareengineering.stackexchange.com/questions/103914/why-and-when-should-i-make-a-class-static-what-is-the-purpose-of-static-key

What is the java equivalent bounded wildcard in C# properties? [duplicate]

This question already has answers here:
C#'s equivalent of Java's <? extends Base> in generics
(4 answers)
Closed 9 years ago.
I’m looking for equivalent C# code of following line
private List<? extends HotSpot> hotSpots;
Any help is much appreciated.
Depending on exactly what you need, you're probably either looking for:
public class MyClass
{
private IList<HotSpot> hotSpots;
}
or using where
public class MyClass<T> where T : HotSpot
{
private IList<T> hotSpots;
}
Functionally, I'd say the closest is:
IEnumerable<HotSpot> hotSpots;
If the real type of the the enumerable happens to be an IList, then ElementAt() and such will be O(1).
As of .NET 4.5, you could also use:
IReadOnlyList<HotSpot> hotSpots;
and use List.AsReadOnly() to wrap regular lists.
The .NET approach to variance in generics is letting specific interfaces be either covariant or contravariant, and consequently only allow them to define methods with the generic type parameter as the return value only, or only in the argument list. (As opposed to Java where the compiler does these checks in each expression.) My guess is the rationale is that C# implements generics using reification, and a concrete type like List<out T> can't exist in the type system.

implementing a custom cast for two types in c# [duplicate]

This question already has answers here:
C# adding implict conversions to existing types
(2 answers)
Closed 9 years ago.
I have two custom classes for which I want to implement casts between each other. I only have the DLLs of the two projects and not the code. Can I use extension methods to implement the cast or would I need to do something else?
I'd suggest that you implement your own mappers between the 2 classes or use mapping tools such as AutoMapper or ValueInjecter
You will have to use either extension methods or some other mapping. You could also use http://automapper.codeplex.com/
I don't think there is a way to do it. Anyway, do you really need the code to look like cast? Sometimes when you implement operators or casts for custom types the code may become harder to understand. I would suggest to create separate utility to convert types which would be more obvious for someone who sees the code for the first time.

Categories