By 22/07/2015 12 Comments

C# Static Classes

  • In C# object oriented programming classes can also be created / declared as static by prefixed them with keyword static. Static class is required for creating an extension method.
  • No object of a static class can be created and a static class must contains only static members (methods and variables).
  • When we create any static class we must explicitly marks its all members (methods and variables) with keyword static and make them static members of a class.
  • Static class does not contain non-static members because static class prevents us from non-static member’s declaration.

Example:

using System;
namespace csharpBasic
{
    // A static class declaration
    static class MarkSheet
    {
        // Declaration of a double type static property TotalMarks.
        public static double TotalMarks
        {
            get
            {
                return 300;
            }
        }

        // Double type static fields or class variables are initialized.
        static double AspMarks = 85;
        static double JsMarks = 60;
        static double CssMarks = 65;

        // Foolowing are double type static methods.
        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 statements print each static method’s result 
               by calling it through class name. */
            Console.WriteLine("Mark sheet:");
            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:
        
        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 .