I will use the names of the classes you put in the question. We have the following classes:
The base class Account
:
internal class Account
{
internal int AccountNumber { get; set; }
}
And two classes they inherit from it. A class for business accounts:
internal class BusinessAccount : Account
{
internal string BusinessName { get; set; }
internal decimal Revenue { get; set; }
}
And one for organizations:
internal class OrgAccount : Account
{
internal string OrganizationName { get; set; }
}
Classes differ in the category of properties, so they can be used in code for more specific and different things.
Make a downcasting is useful in the example of a method GetAccount(int accountNumber)
that can return any type of account, be it BusinessAccount
or OrganizationAccount
.
However, as each one differs in its properties, we need to treat them in different ways. For this we can use the following conversion, as in the example below:
// Não temos certeza de qual é o tipo que está retornando (Account, Business ou Organization)
Account account = GetAccount(accountNumber: 93840303);
// Verificando o tipo que retornou do método GetAccount
if (account is BusinessAccount)
{
// downcast para um tipo mais específico
var businessAccount = (BusinessAccount)account;
// recebe apenas o tipo BusinessAccount
ManageBusinessAccount(businessAccount);
}
else if (account is OrganizationAccount)
{
// downcast para um tipo mais específico
var orgAccount = (OrganizationAccount)account;
// recebe apenas o tipo OrganizationAccount
ManageOrganizationAccount(orgAccount);
}
else
{
//...
}
See that the downcasting is necessary in this scenario since the methods ManageBusinessAccount
and ManageOrganizationAccount
do not receive the base type as argument.
But in case we have a method ManageAccount
which receives only the base type Account
as argument, we can pass any of the types:
OrganizationAccount orgAccount = (OrganizationAccount)account;
ManageAccount(account); // Este método recebe apenas o tipo Account como argumento
So much for the guy BusinessAccount
as to the type OrganizationAccount
implement the base class. That is, the upcasting is "implicit".
Related: What is the purpose of an explicit C interface implementation#?.
– Wallace Maxters
Related: https://answall.com/questions/449584/upcasting-e-downcasting-subsequente-permite-acessar-o-attributedo-typo-original/449650#449650
– Wallace Maxters
Downcasting, mostly because there is design error. The most common when they explain about it teach to design wrong. Yes, most of the libraries and frameworks you use have design error. Almost always have better solution if the project is well done. Some of the answers below teach you to cast where you don’t need to or don’t teach you to cast, or talk about a cast you didn’t need to cast (the question doesn’t help). I am running out of time to prepare a full answer. Relacionados: https://answall.com/q/286192/101 e https://answall.com/q/131091/101.
– Maniero
I will look at all the links and thank you for the effort to help
– Brener Sousa