Windows Phone Developers

Friday, April 9, 2010

Private Constructors in C# / VB.NET

Use of Private Constructors in C# (.NET)

Here is an example of a Private Constructor

public class WicketCounter
        //Private Constructor
        private WicketCounter() { }

        // No of Wickets
        public static int Wickets;
        // Public Method to Return the No of Wickets
        public static int WicketCount()
            return ++Wickets;


The Class has a private constructor, a public static method and a static variable. The Method increments the Wicketcount (and hence the class itself is called wicketcounter) and the value is returned through Wickets var


The declaration of private constructor prevents the creation of default constructor. Hence the class cannot be instantiated and doesn;t contain any instance fields or methods 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