What you're talking about might make sense in other languages. But in C#,
you're never going to call the "inherited" method without explicitly
providing the type itself. Any static method, even one that meets your
hypothetical goal of being virtual, needs to be called via the type name
itself.
You are absolutely right.
In EVERY usage scenario for this the programmer writing the code
definitelly knows the classname. They could even just enter in the
string itself. The reason for providing it as a property is not
because its unknown, but because its an elegant way of converting the
classname to a string, without hardcoding a string.
For example... any situation where the user might use someClass.Name,
they could just as easily use the string "someClass" directly, never
mind having to use a typeof(someClass).Name.
But I don't believe hard coding the strings is good style. You lose
out on intelli-sense and compile time checking. Its also easier to
propagate changes to the class name to dependant code if the class
name isn't hard coded in strings all over the place.
Using typeof(someClass).Name works just fine, benefits from
intellisense, and compile type checking, etc... so it meets those
requirements.
But if its something I need a lot of why wouldn't I want to
encapsulate it into the class itself, so that instead of having the
user of the class reflect its name to a string whenever they need it,
the class provides a method or property that does it.
That's the point of Arne's question. Let's assume you _could_ come up
with a static virtual method that does what you want. You would still
only ever be calling this method by providing the type name itself.
Agreed. Its just about encapsulation and style.
If
you want the "someClass" implementation, then you'd have to write
"someClass.ClassName()" (or IMHO it'd be a property and you'd write
"someClass.ClassName", but whatever).
Agreed. I'd prefer a property to a method.
How is this significantly better than simply writing
"typeof(someClass).FullName"?
That's basically the same as asking:
How is writing myInt.ToString() significantly better than
Convert.ToString(myInt)
Its not that one is 'signficantly better' than the other. The latter
has an external (Convert) class perform the conversion on the int
type, the former encapsulates the conversion into the int type. Two
different ways of doing the same thing. Nothing wrong with either. And
indeed, for all i know myInt.ToString() is implemented as
Convert.ToString(this) behind the scenes.
That's not really the issue.
In this case, because the the class names will be frequently needed it
makes aesthetic sense to have the class provide its own name, rather
than require the programmers to reflect out themselves each time. Just
like it makes sense for Int to be able to convert itself to a string
without the caller manually invoking a helper class each time.
Either will work. It just seems odd that this particular bit of
functionality can't easily be encapsulated.
cheers,
-Dave