By 22/07/2015 3 Comments

C# Static Members

  • Like instance or non-static members (methods and fields), a class can also contains static members in C#.
  • Static members (methods and fields) can be created by mark a keyword static as prefix.
  • Static methods and variables do not associated with a particular object. These are directly associated with a class itself and are free, they are also called class members.
  • Static members (methods and fields) can only be called through its class reference, and cannot be called through an object reference.
  • Static members are being initialized or created before object creation or instance initialization because a static member loads into memory at compile time while instance or an object creates at runtime.
  • Static members are independent from any specific object.

Example, a mark sheet program:

using System;
namespace csharpBasic
{
    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 %
    
       */
    }
}


REMEMBER:

  • Non-static or instance methods and fields cannot be accessed/call within static methods but it can be possible that non-static methods and variables can be called/accessed within static methods through only its class object reference.





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 .