-1

I'm working on a calculator that uses polynomials, and I was wondering if I could make some sort of "custom type" to use instead of and array of lists of strings.

So, for example, instead of List<string>[] polynomial I'd like to write Polynomial polynomial, where the Polynomial type is a List<string>[] (which means I could use it the same exact way I currently use the List<string>[], so, for example, I could access the values with polynomial[n][i] like I do now)

Is there any way to do this?

2 Answers2

-1

As suggested by @BionicCode I used an inheritant Polynomial class

public class Polynomial : List<List<string>>
{

}

and it works as expected. I did have to change List<string>[] to List<List<string>> beacuse otherwise it would throw a "invalid base type" error, but it still functions the same way:

Polynomial polynomial;

to create a new variable and

polynomial[n][i]

to access its values.

To instantiate a new Polynomial I use

polynomial = new Polynomial();
    
for (int i = 0; i < length; i++)
{
    polynomial.Add(new List<string>());
}
  • It's not a great solution because the outside world can manipulate your polynomial's internals. You don't want that. `public class Polynomial { private List[] internals; }` is a better approach. – Enigmativity Mar 27 '22 at 10:17
  • https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt has over 1500 upvotes and the answer over 1700 upvotes :) – tymtam Mar 27 '22 at 11:32
  • @tymtam - And the answer says NOT to inherit from list. – Enigmativity Mar 29 '22 at 06:10
  • I'm pretty sure this answer shows inheriting from a list. – tymtam Mar 29 '22 at 06:37
  • @tymtam - Yes, it does, but the text says not to. "`List` is a mechanism. Football team is a business object -- that is, an object that represents some concept that is in the business domain of the program. Don't mix those!" – Enigmativity Mar 29 '22 at 07:04
  • @tymtam - "So make a property called `Roster` that is a `List`." – Enigmativity Mar 29 '22 at 07:05
  • @tymtam - "You spent more time typing up your question that it would have taken you to write forwarding methods for the relevant members of `List` fifty times over. You're clearly not afraid of verbosity, and we are talking about a very small amount of code here; this is a few minutes work." – Enigmativity Mar 29 '22 at 07:06
  • @tymtam - "I gave it some more thought and there is another reason to not model a football team as a list of players." – Enigmativity Mar 29 '22 at 07:06
  • @Enigmativity I think we have a case of a bad comment and a mistaken identity. I'm not the OP and my comment was meant to mean that this solution is bad. – tymtam Mar 29 '22 at 07:07
  • 1
    @tymtam - Sorry if I've misunderstood. I thought you were suggesting that the linked answer was supporting inheritance. – Enigmativity Mar 29 '22 at 07:10
-1

Use the following code for Polynomial class.

public class Polynomial
{
    public List<Monomial> monomials = new List<Monomial>();

    public Polynomial(List<Monomial> allMonomials)
    {
        monomials = allMonomials;
    }

    public Polynomial()
    {
        monomials = new List<Monomial>();
    }
}

And use the code below for Monomial class.

    public class Monomial
{
    public List<string> items = new List<string>();

    public Monomial()
    {
        items = new List<string>();
    }

    public Monomial(List<string> allItems)
    {
        items = allItems;
    }
}

To sum up, you can create new objects of Monomial and Polynomial as simple as you can see in the following code

List<Monomial> monomials = new List<Monomial>();
monomials.Add(new Monomial(new List<string>() {"A", "B", "C"}));
monomials.Add(new Monomial());
Polynomial polynomial = new Polynomial();
Polynomial polynomial2 = new Polynomial(monomials);
Abolfazl
  • 100
  • 1
  • 9
  • What does this give you that `List[]` doesn't? – Enigmativity Mar 27 '22 at 10:46
  • I mean I get the same result just by writing `List[] polynomial = new[] { new List() { "A", "B", "C" } };`. – Enigmativity Mar 27 '22 at 10:48
  • @Enigmativity that's semi true. Actually it is `List>` so more dynamic and .. well, tbh, it is exactly what OP is asking for ^^ You could still add the relevant methods/properties/indexer for adding, editing items safer without directly exposing the lists themselves etc but basically that's it – derHugo Mar 28 '22 at 10:32
  • @derHugo - What the OP asked for and what's a good idea might be two different things. – Enigmativity Mar 28 '22 at 20:28
  • @Enigmativity that's true .. but this doesn't mean this answer is bad per se .. it is the question/OP's intent that is probably bad but this answer (as the other one) valid attempts to answer this .. whether they are highly controversial is another question ;) – derHugo Mar 29 '22 at 05:53