locked
What is the Use of a Nameless List In Void Method RRS feed

  • Question

  • User931778073 posted

    Usually I see a List instantiated using the "new" keyword before it is used. However, I came across  a method where "List.Add" is used without instantiating the List first like in the following method:

    public void DoSomething(string input)
    {  
      List.Add(input);
    }

    My question is how is this List used or accessed later. 

    Wednesday, February 12, 2020 5:09 AM

All replies

  • User288213138 posted

    Hi ProgMaster,

    ProgMaster

    public void DoSomething(string input)
    {  
      List.Add(input);
    }

    My question is how is this List used or accessed later. 

    How would you like to use List? Get the value in it? 

    If so, you can try to use the for loop.

    foreach (string s in list)
          {
            Response.Write(s);
           }

    More information about the use of list you can refer to this link: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=netframework-4.8

    Best regards,

    Sam

    Wednesday, February 12, 2020 5:40 AM
  • User931778073 posted

    Hi Sam,

    Thanks for replying but I don't think you understood my question. If you've noticed, the method is a void method and List isn't being used to update a variable or anything else. So my question is what good is it if the method DoSomething does not return any value when it's called. Why use List in this way if you cannot access it by calling the DoSomething method?

    Wednesday, February 12, 2020 8:04 AM
  • User288213138 posted

    Hi ProgMaster,

    So my question is what good is it if the method DoSomething does not return any value when it's called.

    Do you want to know difference between void method and return value method?

    If so, I can only say that they have various application scenarios. for example, The following code shows that they are the same function.

     public class pro
        {
            int result;
            int result2;
    
            public void Add(int num1, int num2)
            {
    
                result = num1 + num2;
            }
    
            public int Add2(int num1, int num2)
            {
    
                return result2 = num1 + num2;
            }
    
    
            public static void Main(string[] args)
            {
                pro p = new pro();
    
                p.Add(1, 2);
                Console.WriteLine(p.result);
    
                p.Add2(1, 2);
                Console.WriteLine(p.result2);
                
            }
        }

    Why use List in this way if you cannot access it by calling the DoSomething method?

    Can you show me how did you access the List?

    Best regards,

    Sam

    Wednesday, February 12, 2020 9:07 AM
  • User753101303 posted

    Hi,

    Use "Go to definition" in VS.

    Seems you are confused because of the name but it can still be a variable declared and instantiated somewhere else which is likely. It could be static class but IMO less likely.

    You have to check the definition for List. As shown this code won't compile... For example I expect code such as :

    using System.Collections.Generic;
    
    namespace ConsoleDemo
    {
        class Program
        {
            class Demo
            {
                private List<string> List = new List<string>();
                public void DoSomething(string input)
                {
                    List.Add(input);
                }
            }
            static void Main(string[] args)
            {
                var d = new Demo();
                d.DoSomething("Test");
           }
        }
    }

    Or yet another option :

    namespace ConsoleDemo
    {
        class Program
        {
            static class List
            {
                public static void Add(string input) {
                    // some actual usefull code
                }
            }
            class Demo
            {
                public void DoSomething(string input)
                {
                    List.Add(input);
                }
            }
            static void Main(string[] args)
            {
                var d = new Demo();
                d.DoSomething("Test");
           }
        }
    }

    As you see what matters is not really how List is used inside DoSomething but rather what it IS and so you have a look at this declaration.

    Wednesday, February 12, 2020 9:08 AM