locked
About constructors... RRS feed

  • Question

  • User211148926 posted
    // Namespace Declaration
    using System;
    
    // helper class
    class OutputClass 
    {
        string myString;
    
        // Constructor
        public OutputClass(string inputString) 
        {
            myString = inputString;
        }
    
        // Instance Method
        public void printString() 
        {
            Console.WriteLine("{0}", myString);
        }
    
        // Destructor
        ~OutputClass() 
        {
            // Some resource cleanup routines
        }
    }
    
    // Program start class
    class ExampleClass 
    {
        // Main begins program execution.
        public static void Main() 
        {
            // Instance of OutputClass
            OutputClass outCl = new OutputClass("This is printed by the output class.");
    
            // Call Output class' method
            outCl.printString(); 
        }
    }

    What makes the OutputClass method the Constructor method? What differentiates it from a regular method? is it because the method shares the same name as the class?

    Thanks.

    Friday, January 23, 2015 12:17 AM

Answers

  • User509211905 posted

    The default constructor for your OutputClass would look like this: 

    class OutputClass
    {
      // this is a constructor without a parameter. A constructor is defined like a method but it should be the same name of your class
      public OutputClass() 
      {
    
      }
    }
    
    

    To instantiate, it will be something like this: 

    OutputClass oc = new OutputClass();

    // you can't call the constructor like a regular method
    oc.OutputClass();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2015 2:01 AM

All replies

  • User509211905 posted

    A constructor is basically instantiating a class/object for use. It can have parameters in order to set some variables values inside the class. With your example above, it means that when you initialize your OutputClass you need to specify your input string. Regular methods can only be use/called if a class/object has been instantiated (except for static methods). IMO, I wouldn't called it as constructor method, I would simply call it as constructor. 

    Hope that helps.

    Friday, January 23, 2015 1:38 AM
  • User211148926 posted

    Then OutputClass is a constructor only because I define it as so? There's no special syntax that C# interprets as it being a constructor?

    Friday, January 23, 2015 1:55 AM
  • User509211905 posted

    The default constructor for your OutputClass would look like this: 

    class OutputClass
    {
      // this is a constructor without a parameter. A constructor is defined like a method but it should be the same name of your class
      public OutputClass() 
      {
    
      }
    }
    
    

    To instantiate, it will be something like this: 

    OutputClass oc = new OutputClass();

    // you can't call the constructor like a regular method
    oc.OutputClass();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2015 2:01 AM
  • User211148926 posted

    Got it, thanks.

    Friday, January 23, 2015 2:39 AM