Last post Jan 23, 2015 02:39 AM by RaulMarquez
Jan 23, 2015 12:17 AM|RaulMarquez|LINK
// Namespace Declaration
// helper class
public OutputClass(string inputString)
myString = inputString;
// Instance Method
public void printString()
// Some resource cleanup routines
// Program start class
// 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
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?
Jan 23, 2015 01:38 AM|Glenn Saint|LINK
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.
Jan 23, 2015 01:55 AM|RaulMarquez|LINK
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?
Jan 23, 2015 02:01 AM|Glenn Saint|LINK
The default constructor for your OutputClass would look like this:
// this is a constructor without a parameter. A constructor is defined like a method but it should be the same name of your class
To instantiate, it will be something like this:
OutputClass oc = new OutputClass();// you can't call the constructor like a regular methodoc.OutputClass();
Jan 23, 2015 02:39 AM|RaulMarquez|LINK
Got it, thanks.