Saturday, March 26, 2016

C# - Static Elements

Static Variable

When using an object oriented platform, we can create a variable in a class (int, double, char…) that will replicated per instance of the class. The static variables are used to reduce this replication and allowing us to create a single variable that is used at the class level.


In addition :

·       Static variable is created only one time for all instances.
·       When working with different classes we can use the Static variable without creating an instance of the class.
·        Static variables are configured only one time in memory.
·        Calling to a static variable made with "ClassName.Varable".

Example:
In the following code I'll demonstrate a static variable that updated per call no matter the class instance that use it.
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            BankAccounts ClientA = new BankAccounts("Alfred");
            Console.WriteLine(BankAccounts.TotalMonyInBank);
            BankAccounts ClientB = new BankAccounts("Hulk");
            Console.WriteLine(BankAccounts.TotalMonyInBank);
            BankAccounts ClientC = new BankAccounts("Philip");
            Console.WriteLine(BankAccounts.TotalMonyInBank);
            BankAccounts ClientD = new BankAccounts("Alex");
            Console.WriteLine(BankAccounts.TotalMonyInBank);
        }
    }

    class BankAccounts
    {
       public static int TotalMonyInBank = 0;
       
       public string AccountName { get; set; }

       public BankAccounts(string UserAccountName )
       {
           AccountName = UserAccountName;
           TotalMonyInBank = TotalMonyInBank + 100;
       }
    }
}

Result : 









Static Functions:

Static functions are executed on the class level and not for a specific object, therefore like every other component that configured with 'Static' we have few conditions that must be applied before we can access it.

Conditions and Specifications:
·         Not like a NON static function, we can access static function only via Class level.
·         Every function should be declared with the 'static' keyword.
Example:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(StaticExample.Calculation(10,2));     
        }
    }

    class StaticExample
    {
        public static int Calculation(int num1, int num2)
        {       
            return   (num1 * num2)*3;
        }
    }

Result : 








Static Properties

Static property are configured in the same way we configured a variable, all we need to do is to configure the property with  the 'Static' word after the security key.

Non Static Property:

class StaticExample
    {
        //Full Property
        private int example1;
        public int Example1
        {
            get { return example1; }
            set { example1 = value; }
        }

        //Short Property
        public int Example2 { get; set; }
    }

Static Property:

class StaticExample
    {
        //Full Property
        private static int example3;
        public static int Example3
        {
            get { return example3; }
            set { example3 = value; }
        }

        //Short Property
        public static int Example2 { get; set; }
    }


Note!
Like a static variable, we access static Proporty with ClassName.Proporty

class Program
    {
        static void Main(string[] args)
        {
            StaticExample instance1 = new StaticExample();
            //instance1 --we cannot use it to update the static Prop
            StaticExample.Example1 = 50;
            StaticExample.Example2 = 100;
        }
    }

No comments:

Post a Comment

My Presentations