How to initialize a custom variable like this; "S series = new series () {1, 2, 3, 4}"?

I created a class called Serie, it's basically List. How can I make it so that I can make the series like this: "Serie S = new Serie () {1, 2, 3, 4}", how can you do with a list?

    class Serie
{
    public List<decimal> Serie_ { get; set; }

    public decimal this[int index]
    {
        get
        {
            return Serie_[index];
        }
        set
        {
            Serie_.Insert(index, value);
        }
    }

    public Serie()
    {

    }

    public Serie(List<decimal> serie)
    {
        Serie_ = serie;
    }

    public Serie Add(decimal Value)
    {
        List<decimal> lst = new List<decimal>();
        for (int i = 0; i < Serie_.Count; i++)
        {
            lst.Add(Serie_[i]);
        }
        lst.Add(Value);
        Serie S = new Serie(lst);
        return S;
    }

    public double Count()
    {
        return Serie_.Count;
    }

    public static Serie operator +(Serie left, decimal right)
    {
        List<decimal> temp = new List<decimal>();
        for (int i = 0; i < left.Count(); i++)
        {
            temp.Add(left[i] + right);
        }
        return new Serie(temp);
    }
}

      

I get an error when I try to do this;

Cannot initialize type 'with collection initializer because it does not implement' System.Collections.IEnumerable '

I do not know how to do that.

+3


source to share


3 answers


Implement IEnumerable<T>

, try this code:

public class Serie : IEnumerable<decimal>
{
    List<decimal> mylist = new List<decimal>();

    public decimal this[int index]
    {
        get { return mylist[index]; }
        set { mylist.Insert(index, value); }
    }

    public IEnumerator<decimal> GetEnumerator()
    {
        return mylist.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return mylist.GetEnumerator();
    }


    public Serie()
    {

    }

    public Serie(List<decimal> serie)
    {
        mylist = serie;
    }

    public Serie Add(decimal Value)
    {
        mylist.Add(Value);
        return this;
    }

    public double Count()
    {
        return mylist.Count;
    }

    public static Serie operator +(Serie left, decimal right)
    {
        List<decimal> temp = new List<decimal>();
        for (int i = 0; i < left.Count(); i++)
        {
            temp.Add(left[i] + right);
        }
        return new Serie(temp);
    }
}

      



and also you might want an example using the above class:

    static void Main(string[] args)
    {

        Serie s = new Serie();
        s.Add(1).Add(2);
        for (int i = 0; i < s.Count(); i++)
        {
            Console.WriteLine(s[i]);
        }
        Console.ReadLine();
    }

      

+4


source


Technically all you need to do is implement the appropriate method Add

:

 public class Serie {
   //TODO: add required stuff here

   public void Add(int value) {
     //TODO: add required stuff here
   }
 }

 ...
 // Add method lets you use the syntax sugar 
 Serie S = new Serie() { 1, 2, 3, 4 };

      



However, since it Serie

looks like an extension / wrapper in the class List<T>

, it is also wise to implement some interfaces, e.g.

  IEnumerable<Decimal>
  IReadOnlyList<Decimal>

      

+1


source


You must implement IEnumerable<decimal>

and implement the Add (decimal value) method - which you have already done.

But as you go through your code, you should reconsider what you want to do. This looks a little unstructured to me.

0


source







All Articles