Another option
public class Base
{
public int Id { get; set; }
}
Your classes
public class Customer : Base
{
public string Name { get; set; }
}
public class Order : Base
{
public string Product { get; set; }
}
Common class
public class Operations
{
public static string Common(object sender) => sender switch
{
Customer customer => $"Customer id: {customer.Id} Name: {customer.Name}",
Order order => $"Order id: {order.Id} Product: {order.Product}",
_ => "Unknown"
};
}
Now the above is conceptual, this may make more sense if not use to a switch expression
public static string Common(object sender)
{
var result = "";
if (sender is Customer customer)
{
result = $"Customer id: {customer.Id} Name: {customer.Name}";
}
else if (sender is Order order)
{
result = $"Order id: {order.Id} Product: {order.Product}";
}
return result;
}
Test it
Customer customer = new Customer() {Id = 1, Name = "Adams"};
Debug.WriteLine(Operations.Common(customer));
Order order = new Order() {Id = 12, Product = "Phone"};
Debug.WriteLine(Operations.Common(order));
When using a switch expression or if statement you can call on say validation methods and or perform actions.
EDIT
Here the DoSomethingWith can be expanded to do whatever and if an unknown type is passed throw an exception
public class Operations
{
public static string Common(object sender) => sender switch
{
Customer customer => DoSomethingWithCustomer(customer),
Order order => DoSomethingWithOrder(order),
_ => throw new Exception("Unknown type")
};
public static string DoSomethingWithCustomer(Customer customer) => $"Customer id: {customer.Id} Name: {customer.Name}";
public static string DoSomethingWithOrder(Order order) => $"Customer id: {order.Id} Product: {order.Product}";
}