Windows Phone Developers

Thursday, October 30, 2008

How to create Default Constructors in C#

Default constructors are the ones that takes no parameters. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.

Constructors have the same name as the class, and usually initialize the data members of the new object.

class ClsTea : IRetail

{

//Variables

private string _locale;

private static string _brand;

public ClsTea()

{

_brand = "N/A";

}

}

The following snippet invokes the default constructor

ClsTea MyTea = new ClsTea();

Console.WriteLine("My default tea brand is " + ClsTea.Brand);

ClsTea.Brand = "Tata";

Console.WriteLine("My favourite tea brand is " + ClsTea.Brand);

Default Constructors in .NET, Default Constructors in .C#, C# Default Constructors, C# New Operator, .NET New Operator, New Operator and Default Constructors Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment