Pass different type of objects to the same method

Sriram 1 Reputation point
2021-09-29T04:09:16.917+00:00

Hi All,

I have a method commonMethod which takes Customer object as param. The logic is same for both Customer and Order class. Instead of rewriting the same code for Order class how to reuse it.

public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}

public class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

public string commonMethod(Customer c)
{
common logic for Customer and Order objects
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,190 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 111.7K Reputation points
    2021-09-29T06:06:22.573+00:00

    I think that it depends on details. For example, you can write this:

    public string commonMethod( Customer c)
    {
        return commonMethod( c.ID, c.Name);
    }
    
    public string commonMethod( Order o)
    {
        return commonMethod( o.ID, o.Product);
    }
    
    private string commonMethod( int ID, string text)
    {
        common logic . . .
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Timon Yang-MSFT 9,571 Reputation points
    2021-09-29T07:58:28.277+00:00

    You can consider using generics.

        public static class MyClass<T>  
        {  
            public static string CommonMethod(T t)  
            {  
                .......  
            }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Karen Payne MVP 35,026 Reputation points
    2021-09-29T09:24:27.023+00:00

    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}";
    }
    
    0 comments No comments