Wednesday, 4 April 2012

OOPS Concept -2 (All about Constructor)

Constructor  ------------------------>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOOPSConcept
{
    /// <summary>
    /// Private Constructor ***************
    /// It is generally used in classes that contain static members only
    /// If u don't declare the modifier with constructor, By Default costructor modifier are "private".
    /// If class contains only private constructor, its clear that the class can not be instantiated.
    /// If all the methods in the class are static, consider making the complete class static.
    /// </summary>

    public class Counter
    {
        private Counter() {
        }
        public static int currentCount;
        public static int IncrementCount()
        {
            return ++currentCount;
        }
     
    }
 
    class TestCounter
    {
        //static void Main()
        //{
        //    // If you uncomment the following statement, it will generate
        //    // an error because the constructor is inaccessible:
        //    // Counter aCounter = new Counter();   // Error    
       
        //    Counter.currentCount = 100;
        //    Counter.IncrementCount();
        //    Console.WriteLine("New count: {0}", Counter.currentCount);

        //    // Keep the console window open in debug mode.
        //    Console.WriteLine("Press any key to exit.");
        //    Console.ReadKey();
        //}
    }
}




No comments:

Post a Comment