0

My goal is to create a imperative program in C# and I know how to go about just using a for loop or while loop etc. But I was told they way I'm using my data/getting the data which is not imperative? I want to have something like a list of lists? and doing so this way:

static void Main()
    {
        List<string> list1 = new List<string> {"asd","asfasf","asfasf"};
        List<string> list2 = new List<string> {"asd","asfasf","asfasf"};
        List<string> list3 = new List<string> {"asd","asfasf","asfasf"};
        List<string> list4 = new List<string> {"asd","asfasf","asfasf"};
        List<string> list5 = new List<string> {"asd","asfasf","asfasf"};


        List<List<string>> lists = new List<List<string>> {list1,list2,list3,list4,list5};


    }

I am not sure if this is an imperative approach to doing so or not? I had this before which i know is basically the exact same:

 static void Main()
            {
               List<List<string>> lists = new List<List<string>>();
        lists.Add(new List<string> {"asd","asfasf","asfasf"});

}

But was told it wasn't as it was using parametric polymorphism which is OOP. I was told for likes of python and that that we couldn't use Lists unless we write our own list declaration and functioned. We also couldn't use built-in functions(e.g .split())

K.Madden
  • 353
  • 1
  • 16
  • 8
    How should we know? Ask the guy that told you to do that in "an imperative way", or what he/she means by that term. – MakePeaceGreatAgain Nov 20 '19 at 13:55
  • 1
    Were you also told why your approach was not imperative? – Pedro Isaaco Nov 20 '19 at 13:56
  • tell him I'm using C# which is an **imperative** language though it contains some functional principles – canbax Nov 20 '19 at 13:57
  • 1
    `new List {"asd","asfasf","asfasf"};` is just a *syntax sugar* with `Add("asd"); ... Add("asfasf");` etc. under the hood – Dmitry Bychenko Nov 20 '19 at 13:58
  • See above for reason why I'm unsure – K.Madden Nov 20 '19 at 14:02
  • I suspect that there is more to the code than just this? What do you do with the list after it's created? – koolahman Nov 20 '19 at 14:17
  • I havn't done the rest yet as I know how to go about that 'imperatively' but it will just be likes of 2 for loops and using a counter and outputing the length of each list in a list. That's all, nothing major. Using a counter as i assume using a built-in function like '.count' is not imperative according to the person asking – K.Madden Nov 20 '19 at 14:18
  • 1
    When someone gives you instructions you don't understand, you need to ask *them* to explain further. –  Nov 20 '19 at 14:34
  • 1
    I really cannot say for sure, but I'm guessing they are saying the first attempt is not imperative because the list is created all in the same step as declaring it, whereas the second attempt explicitly adds each item in a step-by-step basis. This post doesn't necessarily answer your question, but is a great reference. I quote from the accepted answer "With imperative programming, you tell the compiler what you want to happen, step by step." https://stackoverflow.com/questions/1784664/what-is-the-difference-between-declarative-and-imperative-programming – koolahman Nov 20 '19 at 14:38

0 Answers0