Wednesday, May 23, 2012

Destructors

Destructors are use to clear the memory space used by the class
It is denoted by using~  to the constructors

The program to understand a destructor is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace destructors
{
    class calculator//constructor
    {
        int i, j,k;
        calculator()
        {
            i = 25;
            j = 78;
            Console.WriteLine("The Values that are initialized by the are {0},{1}",i,j);
        }
        ~calculator()//destructor
        {
            Console.WriteLine("The Values are removed");
        }
        void addnumber()
        {
            k = i + j;
            Console.WriteLine("The added value is {0}",k );
        }
        void display()
        {
            Console.WriteLine("The Value of i and j is {0},{1}",i,j);
        }
        static void Main(string[] args)
        {
            calculator d1= new calculator();//the constuctor is called
            d1.addnumber();//the function is called but at the same time the destructor will also be called
            d1.display();
            Console.ReadLine();
        }
    }
}
P.S: The Values are cleared only after we exit after runtime so we cannot see the out put in visual studio.So u can use visual stydio command prompt

Constructors

What Is a Constructors??
  1. Constructor is Basically a function
  2. But this function has a name same as that of class name
  3. Constructors has no return types
  4. The Constructor is called when the instance of the class that is the object is created.
We can say that only use of constructors is to initialize the values whenever an object is called
To understand the concept we will be looking at two programs in this blog.
  • Program 1:
               In this program we will be explicitedly create a function that will initialize the value whenever the function is called.

  • Program 2:
              In this program we will be creating a constructor that will automatically initialize the value at the time of creation of class without even calling it.

  • Program 3:
               In this program we will create constructors with parameters.We will just modify the Program 2 not a big deal


So Here goes the Program 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace constructors1
{
    class check
    {
        int num1, num2,num3;
        void initialize() //We are creating a function that will initialise the values
        {
            num1 = 20;
            num2 = 30;

        }

        void add_nunmbers()
        {
            num3 = num1 + num2;
        }

        void display()
        {
            Console.WriteLine("The addition of the numbers 20 and 30 is {0}",num3);
        }
        static void Main(string[] args)
        {
            check c1=new check();
            c1.initialize();//We are calling the function to initialize
            c1.add_nunmbers();
            c1.display();
            Console.ReadLine();
           
        }
    }
}



Program 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace constructors1
{
    class Constructor
    {
        int num1, num2, num3;
        Constructor() //We are creating a function that has the name same as that of the class this will initialise the values
        {
            num1 = 20;
            num2 = 30;

        }

        void add_nunmbers()
        {
            num3 = num1 + num2;
        }

        void display()
        {
            Console.WriteLine("The addition of the numbers 20 and 30 is {0}", num3);
        }
        static void Main(string[] args)
        {
            Constructor c1 = new Constructor();//This will automaitcally initialize the values
          
            c1.add_nunmbers();
            c1.display();
            Console.ReadLine();

        }
    }
}






Program 3: Here we will be using inputs from the user.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace constructors1
{
    class Constructor
    {
        int num1, num2, num3;
        Constructor(int number1,int number2) //We are creating a constructor with parameter
        {
            num1 = number1;
            num2 = number2;

        }

        void add_nunmbers()
        {
            num3 = num1 + num2;
        }

        void display()
        {
            Console.WriteLine("The addition of the numbers {0} and {1} is {2}",num1,num2, num3);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Two Numbers to be Added");
            int n1, n2;
            n1 = Convert.ToInt32(Console.ReadLine());
            n2 = Convert.ToInt32(Console.ReadLine());
            Constructor c1 = new Constructor(n1,n2);//caling the constructors with parameters
            c1.add_nunmbers();
            c1.display();
            Console.ReadLine();

        }
    }
}



Tuesday, May 22, 2012

The Below Is the program To Print Reverse of an Array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace array_reverse
{
    class Program
    {

        public static string reverse(char[] mystring)
        {
               
                for (int i =mystring.Length -1; i >= 0; i--)
                {
                   
                   Console.Write(mystring[i]);
                }
           
          
            return mystring.ToString();
          
        }
        static void Main(string[] args)
        {
            char[] arr = new char[20];
            Console.WriteLine("Enter Elements to be reversed");
            arr=Console.ReadLine().ToCharArray();
            Console.WriteLine("The Reverse of the array is");
            reverse(arr);
            Console.ReadLine();
        }
    }  
    }




Program To Display Prime numbers less than 100

This program is very useful for the begginers of C# to learn for Loop. Let us Answer the basic Question What is Prime Numbers Prime numbers are the numbers that are divisible only by itself and by one.
So here are the steps 

  1. First of all i will take a for loop that will run for all values between 1 to 100 these values are stored in the variable i
  2. Then i am taking a for loop that will take vaues between 2 and i. i will store this in variable k
  3. I this step i will take mode of i and k and store it in a variable rem 
  4. Count1 which will count the number of times the modulus is zero
  5. Then i am taking count2 this calculates the number of times mod is not zero
  6. If the number is prime then its mod to k will never be 0 so  i take a if loop and check the condition whether i has mod 0 . if it has never had a mod zero then it is a prime number
  7. Hence display the number  
 Please do comment in case of any doubts or any alternate programs


The Below Program Is Runned Using Visual Studio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime_number_series
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                int count1 = 0;
                int count2 = 0;
                for (int k = 2; k < i - 1; k++)
                {
                    int rem = i%k;
                    if (rem == 0)
                    {
                        count1++;
                    }
                    else
                    {
                        count2++;
                    }
                }
                if (count1 == 0)
                {
                    Console.Write("{0} \t",i);
                }
               
            }

            Console.ReadLine();
        }
    }
   
}
Output


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime_number_series
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a Number to Be Checked Whether It Is Prime Or Not");
            int i = Convert.ToInt32(Console.ReadLine());
           
            {
                int count1 = 0;
                int count2 = 0;
                for (int k = 2; k < i - 1; k++)
                {
                    int rem = i%k;
                    if (rem == 0)
                    {
                        count1++;
                    }
                    else
                    {
                        count2++;
                    }
                }
                if (count1 == 0)
                {
                    Console.Write("The Value You Have Entered Is A Prime Number");
                }
                else
                {
                    Console.WriteLine("The Value You Have Entered is Not a Prime number");
                }
                Console.ReadLine();
               
            }
          
        }
    }
   
}