C# 4.0 : Add a Constructor

10/9/2010 3:54:18 PM
Define a special method, called a constructor, with the same name as the class, with no return type. A constructor runs when a type is created—it is never called directly.

Here are two constructors for the Vertex3d class—one taking arguments, the other performing some default initialization.

class Vertex3d
public Vertex3d()
_x = _y = _z = 0.0;

public Vertex3d(double x, double y, double z)
this._x =x;
this._y = y;
this._z = z;


Constructors do not need to be public. For example, you could make a protected constructor that is only accessible from derived classes. You could even make a private constructor that prevents instantiation (for utility classes) or is accessible only from other methods in the same class (static factory methods, perhaps).

Top 10
Home Theatre Pc Software And Operating Systems (Part 4) - XBMC
Home Theatre Pc Software And Operating Systems (Part 3) - Setting Up Windows Media Center
Home Theatre Pc Software And Operating Systems (Part 2)
Home Theatre Pc Software And Operating Systems (Part 1) - Windows Media Center
Nokia's Extreme Megapixel Bid
Storage, Screens And Sounds (Part 3)
Storage, Screens And Sounds (Part 2)
Storage, Screens And Sounds (Part 1)
Microsoft ASP.NET 4 : Using the SqlProfileProvider (part 4) - The Profile API, Anonymous Profiles
Microsoft ASP.NET 4 : Using the SqlProfileProvider (part 3) - Profiles and Custom Data Types
Most View
Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
Binding Application Data to the UI objects in Silverlight
iPhone Application Development : Getting the User’s Attention - Generating Alerts
Understanding and Using Windows Server 2008 R2 UNIX Integration Components (part 2)
iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
Cisco Linksys X3000 - The Link to Connectivity
HP LaserJet Pro CM1415fnw - Print from The Clouds
Building Your First Windows Phone 7 Application (part 2) - Using Your First Windows Phone Silverlight Controls
Determine Your Need for Server Core
Mobile Application Security : Bluetooth Security - Overview of the Technology
Using System Support Tools in Vista
Windows 7 : Using Windows Live Calendar (part 3) - Scheduling Appointments and Meetings & Viewing Agendas and Creating To-Do Lists
Advanced ASP.NET : The Entity Framework (part 3) - Handling Errors & Navigating Relationships
Graham Barlow: the Apple view
Ipad : Presentations with Keynote - Adding Transitions (part 2) - Object Transitions
Windows Server 2003 : Troubleshooting Group Policy
Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
Building the WinPE Image
Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
Windows Phone 7 Development : Using Culture Settings with ToString to Display Dates, Times, and Text