none
Beginner help

    Question

  • Hi,

    I'm very much a beginner in C#. I'm currently learning from a beginners book and slowly working my way through the various concepts and examples.

    I'm trying to call a method in Main, but I can't figure out why it won't work. The book is a little unclear and I'm getting a bit lost. Here's what I have:

                                       

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication10
    {
        class Program
        {

            public void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element is {0}", a[0]);
                int[] myArray = { 1, 2, 3, 4, 5 };
                PrintFirstElement(myArray);
            }



            public void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine("The first list element is {0}\n", a[0]);
                List<int> myList = new List<int> { 1, 2, 3 };
                PrintFirstListElement(myList);
            }




            static void Main(string[] args)
            {
                PrintFirstElement(int[] a);

            }

        }

    }

     I would appreciate it if someone can tell me how to call the PrintFirstElement method in Main. Thanks.

    Friday, April 7, 2017 12:36 AM

Answers

  • Try this 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine($"The first element is {a[0]}");
                int[] myArray = { 1, 2, 3, 4, 5 };
                PrintFirstListElement(myArray.ToList());
            }
    
            public static void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine($"The first list element is {a[0]}");
                List<int> myList = new List<int> { 1, 2, 3 };
            }
            static void Main(string[] args)
            {
                var values = new int[] {1,2,3 };
                PrintFirstElement(values);
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, April 7, 2017 1:05 AM
    Moderator
  • Hi,

    I am just an amateur programmer, but I think I can give you some answers :-)

    I changed your lines a little bit so that they work:

    public class Program
        {
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element in the parameter array is {0}", a[0]);
    
                int[] myArray = { 1, 2, 3, 4, 5 };
                Console.WriteLine("The first element in local array is {0}", myArray[0]);
    
                //this line of code below is something called 'recursion', I suggest to try these things later after understanding more about the basic concepts
                //PrintFirstElement(myArray);
            }
    
            public void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine("The first list element in the parameter list is {0}", a[0]);
    
                List<int> myList = new List<int> { 1, 2, 3 };
                Console.WriteLine("The first list element in the local list is {0}\n", myList[0]);
                //this line of code below is something called 'recursion', I suggest to try these things later after understanding more about the basic concepts
                //PrintFirstListElement(myList);
            }
    
    
            public static void Main(string[] args)
            {
                int[] a = { 6, 7, 8, 9, 10 };
                //Method 1: Use static functions
                PrintFirstElement(a);
    
                List<int> myList = new List<int> { 4, 5, 6 };
                //Method 2: Create a new object an turn function
                new Program().PrintFirstListElement(myList);
            }
        }

    I had to read a few books and some web-casts to understand the basics of this object oriented programming language. - Keep on reading ;-)

    • Marked as answer by Clydey2Times Friday, April 7, 2017 6:37 AM
    Friday, April 7, 2017 1:11 AM
  • Hi Clydey2Times,

    Thank you for posting here.

    For your question, when you invoke PrintFirstElement() in Main() method. You could defined a int [] a in Main() method like the code below. It will output 0.

     static void Main(string[] args)
            {
                int[] i = { 0, 1, 2 };
                PrintFirstElement(i);
                Console.ReadKey();
            }
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element is {0}", a[0]);
                //int[] myArray = { 1, 2, 3, 4, 5 };
                //PrintFirstElement(myArray);
            }

    If you invoke PrintFirstElement(myArray) like below in your PrintFirstElement(int[] a) method, it will in a dead loop like the code below. It will output 1 forever until it get exception.

     static void Main(string[] args)
            {
                int[] i = { 0, 1, 2 };
                PrintFirstElement(i);
                Console.ReadKey();
            }
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element is {0}", a[0]);
                int[] myArray = { 1, 2, 3, 4, 5 };
                PrintFirstElement(myArray);
            }

    Please note that if you use static in your method PrintFirstElement(), you could invoke the method directly in the Main() method.

    If you does not use static in the method PrintFirstListElement(), you could use the following code.

    static void Main(string[] args)
            {
                List<int> a =new List<int> { 5, 6, 7 };
                Program p = new Program();
                p.PrintFirstListElement(a);
                Console.ReadKey();
            }
            public void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine("The first list element is {0}\n", a[0]);
            }

    If I misunderstand what you want, please feel free to let us know.

    Best Regards,

    Wendy


    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.

    • Marked as answer by Clydey2Times Friday, April 7, 2017 6:37 AM
    Friday, April 7, 2017 6:22 AM
    Moderator

All replies

  • Try this 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine($"The first element is {a[0]}");
                int[] myArray = { 1, 2, 3, 4, 5 };
                PrintFirstListElement(myArray.ToList());
            }
    
            public static void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine($"The first list element is {a[0]}");
                List<int> myList = new List<int> { 1, 2, 3 };
            }
            static void Main(string[] args)
            {
                var values = new int[] {1,2,3 };
                PrintFirstElement(values);
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, April 7, 2017 1:05 AM
    Moderator
  • Hi,

    I am just an amateur programmer, but I think I can give you some answers :-)

    I changed your lines a little bit so that they work:

    public class Program
        {
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element in the parameter array is {0}", a[0]);
    
                int[] myArray = { 1, 2, 3, 4, 5 };
                Console.WriteLine("The first element in local array is {0}", myArray[0]);
    
                //this line of code below is something called 'recursion', I suggest to try these things later after understanding more about the basic concepts
                //PrintFirstElement(myArray);
            }
    
            public void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine("The first list element in the parameter list is {0}", a[0]);
    
                List<int> myList = new List<int> { 1, 2, 3 };
                Console.WriteLine("The first list element in the local list is {0}\n", myList[0]);
                //this line of code below is something called 'recursion', I suggest to try these things later after understanding more about the basic concepts
                //PrintFirstListElement(myList);
            }
    
    
            public static void Main(string[] args)
            {
                int[] a = { 6, 7, 8, 9, 10 };
                //Method 1: Use static functions
                PrintFirstElement(a);
    
                List<int> myList = new List<int> { 4, 5, 6 };
                //Method 2: Create a new object an turn function
                new Program().PrintFirstListElement(myList);
            }
        }

    I had to read a few books and some web-casts to understand the basics of this object oriented programming language. - Keep on reading ;-)

    • Marked as answer by Clydey2Times Friday, April 7, 2017 6:37 AM
    Friday, April 7, 2017 1:11 AM
  • Hi Clydey2Times,

    Thank you for posting here.

    For your question, when you invoke PrintFirstElement() in Main() method. You could defined a int [] a in Main() method like the code below. It will output 0.

     static void Main(string[] args)
            {
                int[] i = { 0, 1, 2 };
                PrintFirstElement(i);
                Console.ReadKey();
            }
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element is {0}", a[0]);
                //int[] myArray = { 1, 2, 3, 4, 5 };
                //PrintFirstElement(myArray);
            }

    If you invoke PrintFirstElement(myArray) like below in your PrintFirstElement(int[] a) method, it will in a dead loop like the code below. It will output 1 forever until it get exception.

     static void Main(string[] args)
            {
                int[] i = { 0, 1, 2 };
                PrintFirstElement(i);
                Console.ReadKey();
            }
            public static void PrintFirstElement(int[] a)
            {
                Console.WriteLine("The first element is {0}", a[0]);
                int[] myArray = { 1, 2, 3, 4, 5 };
                PrintFirstElement(myArray);
            }

    Please note that if you use static in your method PrintFirstElement(), you could invoke the method directly in the Main() method.

    If you does not use static in the method PrintFirstListElement(), you could use the following code.

    static void Main(string[] args)
            {
                List<int> a =new List<int> { 5, 6, 7 };
                Program p = new Program();
                p.PrintFirstListElement(a);
                Console.ReadKey();
            }
            public void PrintFirstListElement(List<int> a)
            {
                Console.WriteLine("The first list element is {0}\n", a[0]);
            }

    If I misunderstand what you want, please feel free to let us know.

    Best Regards,

    Wendy


    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.

    • Marked as answer by Clydey2Times Friday, April 7, 2017 6:37 AM
    Friday, April 7, 2017 6:22 AM
    Moderator
  • Hi. Thanks for all the replies. They were very helpful. Much appreciated, folks. 
    Friday, April 7, 2017 6:36 AM