Constructors, Destructors and Static keyword in C#

Constructors: 

1) Constructor is a special type of method, Which will be executed automatically while creating an object 

2) Constructor name must be same as class name without any return type

 

Syntax: 

public classname(args) 

{

 

}

3) Constructor are overlodable 

4)Generally Constructor are used to initialize instance & static variables,  to open files and to open database connections etc..

 

Destructrors: 

1)destructror is a special type of method, Which will be executed automatically while destroying an object 

2) destructror name must be same as class name with a TILD(~) prefix and without return type and without access specifier.

 

Syntax: 

class name() 

{

   .. 

}

 

3) destructror are notoverlodable

4)generally destructror are used to deallocate the memory and to close the files&close the connections etc..

 

Example on constructors & Destructors:

 

Open windows forms application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select windows forms application project 

open toolbox [view->toolbox]

 

place a button (double click on button)

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data;

using System.Drawing; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms;

 

namespace WindowsFormsApplication 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        class Test 

        { 

            public Test() 

            { 

                MessageBox.Show("froms cons"); 

            }

 

            ~Test() 

            { 

                MessageBox.Show("from des"); 

            } 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            Test t1 = new Test(); 

            Test t2 = new Test(); 

            Test t3 = new Test(); 

        } 

    } 

}

 

Execute the project press F5

 

Observations on above program

1)when above project is executed, Then constructors will be executed for 3 times and  destructrors will be executed for 3 times

2) destructrors will be executed immediately ofter the project is closed

3)after destructrors is invoked then with in 2 seconds garbage collector will be loaded into the memory 

4)garbage collector reclaims(clears) the memory allocated for the current project 

5)garbage collection will be done with the help of a predefined class called as system.GC

 

Working with static concepts: 

1)static is a keyword 

2)static keyword can be used with variables constructors, Methods & classes 

3)instance variables will be created separately for every object 

4)static variable will be created only once for all the objects 

5)static variables are sharable by all the objects 

6)static variables will be created while class is loading into the memory, Hence static variable is also called as class variable 

7)static constructors will be executed only once 

8)static constructors and static methods are capable to access only static variables 

9)static methods need to be called with classname.staticmethod() 

10)when ever a class contains all static methods, Then it is recommended to declare that class as static class

 

Example on static variable & static constructors

Open windows forms application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select windows forms application project  

open toolbox [view->toolbox] 

place a button (double click on button)

 

using System;

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms;

 

namespace WindowsFormsApplication8 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        class Test 

        { 

            private static int s; 

            private int i; 

 

            static Test() 

            { 

                s = 0; 

            }

 

            public Test() 

            { 

                i = i + 1; 

                s = s + 1;

 

                MessageBox.Show(i + " " + s); 

            } 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            Test t1 = new Test(); 

            Test t2 = new Test(); 

            Test t3 = new Test(); 

        } 

    } 

}

 

Excute the project press F5

Working with operator overloading: 

1)Microsoft developer + operator only to add numbers and concatenate the strings i.e we cannot use + operator with objects. 

2)Operator overloading is a concept of extending the functionality of an existing operator. 

3)All the operators are overloadable except the following operators 

     .(member access operator) 

:(inheritance operator) 

::(property access operator. 

?:(Ternary operator (or) conditional operator 

 

4)while overloading relational operators, These must be overloaded in pair 

5)to over >,< must be overloaded 

6)to overload >=, <= must be overloaded 

7)to overload ==, != must be overloaded 

8)overloaded operator must be declared as static

 

Syntax to overload an operator 

public static Returntype operator +(args) 

{

}

 

Example on operator overloading:

 

Open windows forms application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select windows forms application project 

open toolbox [view->toolbox]

place a button (double click on button)

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Linq; 

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication9

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        class Emp 

        { 

            private int sal; 

            public Emp(int x) 

            { 

                sal = x; 

            } 

            public Emp() { } 

            public void print() 

            { 

                MessageBox.Show("total:" + sal); 

            }

 

            public static Emp operator +(Emp a, Emp b) 

            { 

                Emp t = new Emp(); 

                t.sal = a.sal + b.sal; 

                return t; 

            } 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            Emp e1 = new Emp(25000); 

            Emp e2 = new Emp(15000); 

            Emp e3 = new Emp(10000); 

            Emp total = new Emp(); 

            total = e1 + e2 + e3; 

            total.print(); 

        } 

    } 

}

 

Execute the project press F5