How do you cast a List of supertypes to a List of subtypes?

Simply casting to List<TestB> almost works; but it doesn’t work because you can’t cast a generic type of one parameter to another. However, you can cast through an intermediate wildcard type and it will be allowed (since you can cast to and from wildcard types, just with an unchecked warning): List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;

C# : ‘is’ keyword and checking for Not

if(!(child is IContainer)) is the only operator to go (there’s no IsNot operator). You can build an extension method that does it: public static bool IsA<T>(this object obj) { return obj is T; } and then use it to: if (!child.IsA<IContainer>()) And you could follow on your theme: public static bool IsNotAFreaking<T>(this object obj) { … Read more

Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL

You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp: # select ‘2010-01-01 12:00:00′::timestamp; timestamp ——————— 2010-01-01 12:00:00 Now we’ll cast it to a date: wconrad=# select ‘2010-01-01 12:00:00’::timestamp::date; date ———— 2010-01-01 On the other hand you can use date_trunc function. The difference between them is … Read more

Does it make sense to use “as” instead of a cast even if there is no null check? [closed]

Your understanding is true. That sounds like trying to micro-optimize to me. You should use a normal cast when you are sure of the type. Besides generating a more sensible exception, it also fails fast. If you’re wrong about your assumption about the type, your program will fail immediately and you’ll be able to see … Read more