Creating Constructors

We've created objects using new like this: Customer customer = new Customer();. If you're an OOP programmer, you know those parentheses after Customer are there for a reason, because when you create an object from a class, you're using the class's constructor. A constructor is a special method that has the same name as the class and returns no value. It is used to initialize the data in the object you're creating. In C#, constructors are declared this way:

[attributes] [modifiers] identifier([formal-parameter-list])
[initializer] { constructor-body }

Here are the parts of this statement:

  • attributes (Optional)— Hold additional declarative information, as we'll see in Chapter 14.

  • modifiers (Optional)— The allowed modifiers ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.