Static



Static data members:-
Ø  Data members are two types:
                     1) Instance data members
                     2) Static data members
1) Instance data members:
Ø  These are accessible with an object only.
Ø  Memory allocation: will be done independently for each object.
2) Static data members:
Ø  These are accessible without an object.
Ø  We can access static data members with its class name.
Ø  Memory allocation: will be done without any object also, when the class is used for the first time.
Steps for working with static data members:
    i) Define a static data member:
             AccessModifier  static  DataType DataMemberName;
    ii) Access it:
            classname.datammbername
Ex:-
Develop a c# console app, to maintain user count in a static data member.

using System;

namespace StaticDataMembersDemo
{
    //user defined class
    class User
    {
        //data members (instance data members)
        public string Username, Password;

        //static data member
        public static int UserCount = 0;
    }

    class Program
    {
        static void Main(string[] args)
        {
            //create objects for User Class
            User u1, u2;
            u1 = new User();
            u2 = new User();

            //increment the count
            User.UserCount++;
            User.UserCount++;

            //show user count
            Console.WriteLine(User.UserCount); //o/p:   2

            Console.ReadKey();
        }
    }
}
Static methods:-

  • Accessible without creating any object for the class
  • Used to do manipulations on the static data members.

Methods are two types:
    1) Instance methods:
Ø  Accessible with object only.
Ø  Used to do manipulations on instance data members.

   2) Static methods:
Ø  Accessible without object also; can be called with its class name.
Ø  Used manipulate static data members only; can't access instance data members.
Ø  Syntax of static method:
    AccessModifier static ReturnDataType MethodName(arguments)
    {
          //some code
     }
Ex:-
Develop a c# console app, to do arithmetical operations based on the user's choice.
using System;

namespace StaticMethodsDemo
{
    //user-defined class
    class MyMath
    {
        //static methods
        internal static double Addition(double a, double b)
        {
            double c = a + b;
            return c;
        }

        internal static double Subtraction(double a, double b)
        {
            double c = a - b;
            return c;
        }

        internal static double Multiplication(double a, double b)
        {
            double c = a * b;
            return c;
        }

        internal static double Division(double a, double b)
        {
            double c =a / b;
            return c;
        }

        internal static double Remainder(double a, double b)
        {
            double c = a % b;
            return c;
        }
    }

    //startup class
    class Program
    {
        static void Main(string[] args)
        {
            //call the static methods directly (without object)
            Console.WriteLine("Enter first number: ");
            double num1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter second number: ");
            double num2 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter choice (+, -, *, /, %): ");
            string ch = Console.ReadLine();

            double result = 0;
            switch (ch)
            {
                case "+": result = MyMath.Addition(num1, num2); break;
                case "-": result = MyMath.Subtraction(num1, num2); break;
                case "*": result = MyMath.Multiplication(num1, num2); break;
                case "/": result = MyMath.Division(num1, num2); break;
                case "%": result = MyMath.Remainder(num1, num2); break;
                default: Console.WriteLine("Unknown symbol"); break;
            }

            Console.WriteLine("result is " + result);

            Console.ReadKey();
        }
    }
}
Static classes:-
    A collection of only static members (static data members + static methods)
    We can't create an object for the static class.because, all members of the class, are accessible without object.
Syntax of static class:
    static class classname
    {
            //static data members
            //static methods
    }
Note:  'this' keyword can't be used in 'static methods'.
Ex:-
Simple demo on static classes.
using System;

namespace StaticClassesDemo
{
    //user-defined static class
    static class Class1
    {
        //static data member
        public static int x = 10;

        //static method
        public static void method1()
        {
            Console.WriteLine("This is method 1");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //call static data members & methods (without object)
            Class1.method1();
            Console.WriteLine(Class1.x);

            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment