none
Recursively method with Static field in C# RRS feed

  • Question

  • Hello,

    It is Recursively method and I want have one whole program and static keyword doesn't work.

    List<List<int>> listPrint // Just be one in the whole program.

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace CSharp
    {
        
        class Program
        {
    
            static void Recursively(List<int> list)
            {
                List<List<int>> listPrint = new List<List<int>>();
    
                if (list.Count == 1)
                {
                    Console.WriteLine(list[0]);
                    return;
                }
                for (int i = 0; i < list.Count - 1; i++)
                {
                    list[i] = list[i] + list[i + 1];
                }
                list.RemoveAt(list.Count - 1);
                if (list.Count != 1)
                    listPrint.Add(list);
                    //Print(list);
                Recursively(list);
            }
    
    
    
            static void Print(List<int> list)
            {
             
                for (int i = 0; i < list.Count; i++)
                {
                    Console.Write(list[i] + " ");
                }
                Console.WriteLine();
            }
            static void Main(string[] args)
            {
                List<int> mylist = new List<int>();
                int i = 0;
                int numberOfElements;
                Console.WriteLine("Number of Elements??");
                numberOfElements = Convert.ToInt32(Console.ReadLine());
    
                while(i < numberOfElements)
                {
                    Console.WriteLine("Number? ");
                    mylist.Add(Convert.ToInt32(Console.ReadLine()));
                    i++;
                }
                Console.WriteLine();
                Recursively(mylist);
    
            }
            }
        }
    


    • Edited by Arash_89 Saturday, October 26, 2019 6:13 AM
    Saturday, October 26, 2019 6:07 AM

All replies

  • This looks like a duplicate of the issue in another thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/4b7a7f90-bd44-4f98-9e86-fa576b5e68b5/overwrite-problem-in-listltlistltintgtgt-in-c?forum=csharpgeneral

    Since that thread has been marked as Answered by the OP, I think this thread
    can be ignored or deleted.

    - Wayne

    Saturday, October 26, 2019 9:02 AM
  • If this truly is a new question then please explain and not just with source code. If there is a language problem then you can write a good explanation in your native language then translate that as best as possible. But as it is, it is not a language problem; there is just not enough of an explanation.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, October 26, 2019 7:19 PM
  • Hi Arash_89, 

    Thank you for posting here.

    I make a test based on your code, and the program works well.

    >> It is Recursively method and I want have one whole program and static keyword doesn't work.

    Could you provide more details about your question? It will help us to analyze your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 28, 2019 2:58 AM
    Moderator

  • Could you provide more details about your question? It will help us to analyze your problem.

    We are waiting for your update.

    Xingyu Zhao

    <sigh

    Did you not even bother to read the replies already posted in this thread?

    If you did, did you not bother to check the other thread to which I posted a link?

    Clearly this issue HAS BEEN ANSWERED in the other thread! There is nothing else
    for which to wait.

    - Wayne

    Monday, October 28, 2019 5:01 AM