By 22/07/2015 8 Comments

C# Static Constructors

  • A class can contains C# static constructors which initialize the class or static members itself and do not initialize an object of a class. Static constructor initializes a class before an object of the class are created.
  • Static constructor cannot be called C# programs, it is called automatically at the compile time during a class loaded first and static constructor are also called before instance or non-static constructor because in general term a static constructor is always executed before an instance/non-static constructor.
  • A static constructor does not have access modifiers because it has default access modifiers.
  • A static constructor cannot have parameters, it is always declared as null parameter constructor.

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        static double AspMarks = 85;
        static double JsMarks = 60;
        static double CssMarks = 65;

        public static double TotalMarks
        {
            get
            {
                return 300;
            }
        }

        // A static constructor declaration.
        static MarkSheet()
        {
            Console.WriteLine("Static mark sheet:");
            AspMarks = 85;
            JsMarks = 60;
            CssMarks = 65;
        }

        // A non-static declaration.
        public MarkSheet()
        {
            Console.WriteLine("Non-static mark sheet:");
        }

        public static double GetAspMarks()
        {
            return MarkSheet.AspMarks;
        }

        public static double GetJsMarks()
        {
            return MarkSheet.JsMarks;
        }

        public static double GetCssMarks()
        {
            return MarkSheet.CssMarks;
        }

        public static double GetObtainMarks()
        {

            return MarkSheet.AspMarks + MarkSheet.JsMarks + MarkSheet.CssMarks;

        }

        public static double GetPercentage(double obtainMarks)
        {
            return (obtainMarks / MarkSheet.TotalMarks) * 100.0;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            /* Following statement is creating a MarkSheet object 
               and a default non-static constructor is also called. */
            MarkSheet markSheet = new MarkSheet();
            Console.WriteLine("Total marks: {0}", MarkSheet.TotalMarks);
            Console.WriteLine("Asp.net marks: {0}", MarkSheet.GetAspMarks());
            Console.WriteLine("Javascript marks: {0}", MarkSheet.GetJsMarks());
            Console.WriteLine("Css marks: {0}", MarkSheet.GetCssMarks());
            Console.WriteLine("Total obtain marks: {0}", MarkSheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", MarkSheet.GetPercentage(MarkSheet.GetObtainMarks()) + " %");
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Static mark sheet:
        Non-static mark sheet:
        Mark sheet:
        Total marks: 300
        Asp.net marks: 85
        Javascript marks: 60
        Css marks: 65
        Total obtain marks: 210
        Percentage: 70 %
    
       */
    }
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .