-1

I've been using C# for a little while now, but mostly in Unity. I've only recently started just simply writing C# code in Visual Studio.

I was simply playing around with implementing a Queue with an array and was doing a bit of research into constructors. In my Queue class I had a constructor that set up an instance for the array itself:

public class Queue
{

    int front = 0;
    int rear = -1;
    int size = 0;
    const int maxSize = 5;
    int[] queue;

    public Queue()
    {
        queue = new int[maxSize];
    }

    //rest of class

}

Then in the class that calls creates a queue and does some testing etc. with it I used a main method:

class program
{
    static void Main()
    {
        Queue myQueue = new Queue();
        myQueue.enQueue(1);
        myQueue.enQueue(2);
        myQueue.enQueue(3);
        myQueue.enQueue(4);
        myQueue.enQueue(5);
        myQueue.enQueue(6);
        Console.WriteLine(myQueue.deQueue());
        Console.WriteLine(myQueue.deQueue());
        myQueue.enQueue(6);
        myQueue.enQueue(7);
        Console.WriteLine(myQueue.deQueue());
        Console.WriteLine(myQueue.deQueue());
        Console.WriteLine(myQueue.deQueue());
        Console.WriteLine(myQueue.deQueue());
        Console.WriteLine(myQueue.deQueue());
        Console.WriteLine(myQueue.deQueue());
        Console.ReadLine();
    }
}

Now my question is what is the difference between these two methods? At the moment to me they are simply "the method that is invoked when the program is initially run" sort of like the equivalent to the Start() method in Unity which is what I'm used to.

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Tom Ryan
  • 397
  • 2
  • 6
  • 26
  • 7
    "the method that is invoked when the program is initially run" describes your program’s `Main` (one function per program!), but not constructors. Constructors run when you use `new` with the type, like `new Queue()` there. – Ry- May 21 '19 at 20:09
  • https://stackoverflow.com/a/14688779/4573703 has a pretty clear explanation on how a main method is used vs constructor – ocean800 May 08 '20 at 07:34

5 Answers5

5

The Main() method is the entry point of the program. While the Constructor (Queue(), in your case) is invoked as soon as an Object of the class (Queue, in your case) is created.

Vandit Goel
  • 620
  • 2
  • 9
  • 19
1

This answer depends on Microsoft's C# programming guides Main() and command-line arguments and Constructors

Main Method

The Main method is the entry point of a C# application. (Libraries and services do not require a Main method as an entry point.) When the application is started, the Main method is the first method that is invoked.

There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point. For more information, see /main (C# Compiler Options).

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}

The Constructor

Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. For more information and examples, see Using Constructors and Instance Constructors.

A constructor is a method whose name is the same as the name of its type. Its method signature includes only the method name and its parameter list; it does not include a return type. The following example shows the constructor for a class named Person.

public class Person
{
   private string last;
   private string first;
   
   public Person(string lastName, string firstName)
   {
      last = lastName;
      first = firstName;
   }
}
Community
  • 1
  • 1
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
0

The main underlying difference between main method or any other methods and constructor is that method is used to exhibit the functionality of an object whereas constructor is used to initialize an object. In your code, you have a Queue class, a Queue constructor (constructor should always have the same name as class), and a main method in program class. The main method is where the program starts. In the main method, you have initialized myQueue of Queue datatype. That is where the constructor comes into effect. It initializes a new Queue of size max_size, 5 in your case. That is the only function of constructors. However, main method is not done yet. It calls enQueue method and then prints on console and calls deQueue and prints again and at last reads the output.

Amrit Subedi
  • 431
  • 3
  • 11
0
  1. "Main" method is always static WHEREAS "Constructor" can either be static or non-static.
  2. "Main" method is called when project/application first runs WHEREAS "Constructor" is called every time an object is created (when constructor is Non-Static) or static members are accessed the first time (when constructor is Static).
  3. You can have multiple (overloaded) constructors but can not have multiple Main methods set as "Entry Point" of your app/project.
Muhammad Fahad Nadeem
  • 1,120
  • 1
  • 9
  • 15
0

There's no difference >> let me explain ...

When write code with c# and compile it , the compiler will just look at main method..it's Wont look at any another function (method) just the main method , so if you want to make another function you need to call it in main method to make the compiler comeback to this method and run it

Constructor is function (method) you call it when you create object ..just when you create object For Ex : A obj = new A();

focus on : new A(); , here is A() is the constructor , and when you create an object you call the constructor , the constructor has the same name of class , and it's called once...when you create object if you didn't make the constructor the compiler will create one for you and it will be empty you can search more about Constructor .....

E. Zeytinci
  • 2,642
  • 1
  • 20
  • 37
  • You may find [advice for participants who's first language hasn't been English](https://meta.stackoverflow.com/questions/291362/advice-for-non-native-english-speakers) helpful. – greybeard Dec 08 '19 at 18:44