csharp: ran across a quiz

I ran across this quickie C# quiz: Distilled Brilliance :: [Elementary C# Quiz (1): Conversions]

My answers, albeit a little long:

1a) none – nothing requires “u” to be non-null, so T and U can be fully unrelated to each other, you just end up with u == null. There’s certainly no requirement that u be non-null to pass it to the method SomeMethod.

1b) the first reaction would be IV on it being an instance instead of static method, but C# allows properties to have the names of types (which is nice insofar as it makes it more obvious what the type of the property is). So, this could be in a class with a property called “W” of type “W” – hence calling W.SomeMethod actually works fine, since the W will evaluate to the property name, and call the method on that instance. The real answer is again “none”, but that’s more related to whether the question was meant as “with only the snippet code shown, limited to that” or “this could be in a class with other random code causing side effects, like properties with names that match types”

Advertisements

One thought on “csharp: ran across a quiz

  1. Section 14.9.11:
    “Otherwise, the result is the value null with the compile-time type T.”
    This means that U must be related to T in a fashion such that a conversion of a T type to a U type can be done implicitly. This happens in the parent child relationship of T : U. If U is unrelated to T, then you have a compile-time error, as (T)null cannot be converted to a U.

    As far as 1b goes, yes I can see how using properties would allow you to perform such funny little tricks…not sure how kSquared meant it to be interpreted, so we shall see.

Comments are closed.