C
csharper
I have been reading here:
http://forums.asp.net/t/1423519.aspx
The quote of the last but one post says:
"NOTE This action method specifically declares that it returns an
instance of ViewResult. It would work just the same if instead the
method return type was ActionResult (the base class for all action
results). In fact, some ASP.NET MVC programmers declare all their
action methods as returning a nonspecific ActionResult, even if they
know for sure that it will always return one particular subclass.
However, it's a well-established principle in object-oriented
programming that methods should return the most specific type they can
(as well as accepting the most general parameter types they can).
Following this principle maximizes convenience and flexibility for
code that calls your method, such as your unit tests."
I haven't seriously thought about this, yes, it seems to be the case,
since our methods don't always return object. But what's the
rationale of polymorphism? I think polymorphism promotes the use of
more generic types, so, for example, with polymorphism, we do
something like:
Animal dog = new Dog();
assuming that Dog inherits Animal.
A little confused about "returning the most specific type" and
polymorphism.
http://forums.asp.net/t/1423519.aspx
The quote of the last but one post says:
"NOTE This action method specifically declares that it returns an
instance of ViewResult. It would work just the same if instead the
method return type was ActionResult (the base class for all action
results). In fact, some ASP.NET MVC programmers declare all their
action methods as returning a nonspecific ActionResult, even if they
know for sure that it will always return one particular subclass.
However, it's a well-established principle in object-oriented
programming that methods should return the most specific type they can
(as well as accepting the most general parameter types they can).
Following this principle maximizes convenience and flexibility for
code that calls your method, such as your unit tests."
I haven't seriously thought about this, yes, it seems to be the case,
since our methods don't always return object. But what's the
rationale of polymorphism? I think polymorphism promotes the use of
more generic types, so, for example, with polymorphism, we do
something like:
Animal dog = new Dog();
assuming that Dog inherits Animal.
A little confused about "returning the most specific type" and
polymorphism.