Home > database >  How to add a object value to list<> in c# every time object created and print the list at the
How to add a object value to list<> in c# every time object created and print the list at the

Time:10-17

I am new to programming and trying to add object every time object created in list<>.

Customer obj = new Customer(name="abc",price=23);
Customer obj = new Customer(name="efg",price=45);

I want the price and name to add list and would like to sord price using Icomparable interface. Can someone explain it please, Thank You

CodePudding user response:

As per what i understood from your question, you can add a static List into your Customer class and add the price into your list inside the constructor. So every time you create an object of the class, the price will be added to your list.

Customer Class -

public class Customer
{
    public string name { get; set; }
    public int price { get; set; }

    public static List<int> prices = new();
    
    public Customer(string name, int price)
    {
        this.name = name;
        this.price = price;
        prices.Add(price);
    }
}

When you are trying to print -

Customer obj = new Customer(name: "abc", price: 23);
Customer obj1 = new Customer(name: "efg", price: 45);
foreach (var price in Customer.prices)
{
    Console.WriteLine(price);
}

CodePudding user response:

Btw do you know how to compare objects using IComparable interface? I am getting error as other.object is coming null.

I guess you want to compare the price i guess? If yes then id recommend you to do a generic list, the foreach loop adds each item from the string array into the list, where the Constructer is getting passed the line. I "remodelled" your constructor a bit. For comparison you can use the interface IComparable (can also be used with IEnumerable) this is also easier to maintain for you in the future

using System; using System.Collections.Generic; using System.IO;

namespace ConsoleApp25
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = {"abc;23","efg;45" };
            Customer[] customers = ReadFromStrArr(lines);
            Console.WriteLine(customers[0].price.CompareTo(customers[1].price));
        }

        static Customer[] ReadFromStrArr(string[] lines)
        {
            if (lines == null)
                throw new ArgumentNullException(nameof(lines));

            var result = new List<Customer>();

            foreach (var line in lines)
            {
                result.Add(new Customer(line));
            }

            return result.ToArray();
            
        }
    }
    public class Customer : IComparable<int>
    {
        public string name { get; set; }
        public int price { get; set; }

        

        public Customer(string line)
        {

            string[] data = line.Split(";");

            name = data[0];
            price = Convert.ToInt32(data[1]);

            // prices.Add(price); // ??
        }

        public int CompareTo(int other)
        {
            return price.CompareTo(other);
        }
    }
}
  • Related