I have the following example code. Waht is the best practice? Compare Values or compare Types to perform a certain business logic:
public Customer
{
public Category {get;set;}
public CategoryName {get;set;} //e.g. "Category A" or "Category B"
}
public Category{}
public CategoryA : Category {}
public CategoryB : Category {}
public void Main()
{
Customer customer = new Customer();
// Option 1:
if(customer.CategoryName == "Category A")
{
CategoryA catA= customer.Category as CategoryA;
DoSomething(catA)
}
// Option 2:
CategoryA catA= customer.Category as CategoryA;
if(catA != null)
{
DoSomething(catA)
}
// Option 3:
if(customer.Catgeory is Category A)
{
CatgeoryA catA= customer.Category as CategoryA;
DoSomething(catA)
}
}
The Code is just for illustration.