Saturday, 5 April 2014

8: Methods, Static Methods, Static Variables, Constructor, Static Constructor, Destructor, ref and out keyword in C#

Methods

Method implements Class behavior, Methods are functions like in C language but declared in class with access modifiers and may or may not performing operation on class variables.
Methods are block of code that can take parameter and can return a value.

Syntax:
<Access modifier><return type> Name(parameters)

Understanding Methods.

1. Method that takes no parameter, and don’t return any value.


For Example:
A peon, he makes a cup of tea without taking sugar from others. And does not give tea to any others person.

nothing peon(nothing)
{
//making tea
}

Example:
class Mathematic
{
int a = 10;
int b = 15;
public void Sum()
{
Console.WriteLine("sum of two numbers are {0}",a+b);
}
}
class Program
{
static void Main(string[] args)
       {
          Mathematic objMath = new Mathematic();
objMath.Sum();// notice that this method does not take any argument, and
    does not return any value
          Console.ReadLine();
        }
      
}

2. Method that takes parameter, and don’t return any value.

For Example:
A peon, he makes a cup of tea by taking sugar from others, And does not give tea to any other person.

nothing peon(sugar)
{
//making tea
}

Example:
class Mathematic
{
public void Sum(int a, int b)
       {
            Console.WriteLine("sum of two numbers are {0}",a+b);
       }
}
class Program
{
        static void Main(string[] args)
        {
            Mathematic objMath = new Mathematic();
            objMath.Sum(10,15);
            Console.ReadLine();
        }
}

3. Method that takes parameter, and returns a value

For Example:
A peon, he makes a cup of tea by taking sugar from others, and gives tea to any other person.

tea peon(sugar)
{
//making tea
Return tea;
}

Example:
class Mathematic
{
public int Sum(int a, int b)
{
            return a + b;
       }
}

class Program
{
static void Main(string[] args)
       {
            Mathematic objMath = new Mathematic();
            int sum=objMath.Sum(10,15);
            Console.WriteLine("sum of two numbers are "+sum);
            Console.ReadLine();
       }
      
}


Static Methods

Static methods invoke directly by the class name, does not require the object of the class.
Static method is declared using the keyword static. We cannot access static methods by the object of the class.
For example Main method is static that does not require any instance of the class for it to be invoked.

Example:
class Mathematic
{
public static int Sum(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
            int sum = Mathematic.Sum(2,4);
            Console.WriteLine("sum of two numbers are "+sum);
            Console.ReadLine();
        }
      
}

Static Variables

Similar to static method, we can also use Static Variables. Static Variable are special variables that does not require the object of the class.
Static variable is declared by the static keyword, we cannot access static variable by the object of the class.

Example:
class Mathematic
{
public static int num1;
          public static int num2;

public static int Sum()
          {
                             return num1+num2;
          }
}
class Program
{
static void Main(string[] args)
{
            Mathematic.num1 = 5;
            Mathematic.num2 = 4;
            int sum = Mathematic.Sum();
            Console.WriteLine("sum of two numbers are "+sum);
            Console.ReadLine();
       }
      
}

Ref Keyword

We can use ref keyword to pass parameters by reference, any changes made to the parameters in the called method will be reflected in the parameter passed from the calling method when control passes back to the calling method.

Example:
class Mathematic
{

public void refMethod(ref int num1,ref int num2)
{
            num1=num1 - 10;
            num2=num2 - 20;
          }
}
class Program
{
static void Main(string[] args)
       {
            int numOne = 20;
            int numTwo = 30;
            Mathematic objmath = new Mathematic();
            Console.WriteLine("before calling method value of numOne= {0} and numTwo=}", numOne, numTwo);
            objmath.refMethod(ref numOne, ref numTwo);
            Console.WriteLine("After calling method value of numOne= {0} and numTwo={1}",numOne,numTwo);
            Console.ReadLine();
       }
      

}

Output:


Out Keyword

Working of out keyword is similar to working of ref keyword, but the only difference between them that the out keyword does not require the variable to be initialized that passed by reference.

Example:
class Mathematic
{
        public void refMethod(out int num1,out int num2)
{
            num1=10;
            num2=20;
       }
}
class Program
{
        static void Main(string[] args)
        {
            int numOne;
            int numTwo;
            Mathematic objmath = new Mathematic();
            Console.WriteLine("before calling method value of numOne and numTwo are not
                                                            initialized");
            objmath.refMethod(out numOne, out numTwo);
            Console.WriteLine("After calling method value of numOne= {0} and numTwo=
                                                            {1}",numOne,numTwo);
            Console.ReadLine();
        }
      

}


Constructor
  1. Constructor is a special type of method having same name as the class name.
  2. Constructor can initialize the variables of a class.
  3. Default constructor call automatically one time in its life. when we create the object of the class

For example
          Mathematic objmath = new Mathematic();

Red text is called constructor.

·         Constructor does not return any value because its implicit return type is class itself.

Syntax:
<access modifier><Class name>(parameters, if no parameters leave empty)

Example: 1

class Mathematic
{
public Mathematic()
       {
            Console.WriteLine("hi this is Default constructor, Automatically call when object created");
       }
}

class Program
{
static void Main(string[] args)
       {          
            Mathematic objmath = new Mathematic();           
            Console.ReadLine();
       }
}


Ouput:
hi this is constructor, Automatically call when object created.

Example2:

We can also initialize variable by the constructor
class Office
{
public string address;
public string name;

       public Office()
       {
            address = "Tariq road";
            name = "solution";
       }
      
public void OfficeDetail()
        {
            Console.WriteLine("Office Name: {0}\nAddress:{1}\n", name, address);
        }
}
class Program
{
static void Main(string[] args)
       {          
            Office objOffice = new Office();
            objOffice.OfficeDetail();
            Console.ReadLine();
       }
      
}
Example3:

We can also initialize variables by the constructor like this.
class Office
{
        public string name, fatherName, Age;
        public Office(string _name,string _fatherName, string _age)
        {
            name = _name;
            fatherName = _fatherName;
            Age = _age;
            Console.WriteLine("constructor has initialized variables");
        }
}
class Program
    {
        static void Main(string[] args)
        {

            Office objMath = new Office("Ahmad","Majid","23");
          
            Console.ReadLine();
        }
      
    }

Output:
Constructor has initialized variables

Static Constructor

Static constructor is use for initialization of static variables of a class and can perform particular action at once. Static constructor calls automatically when we call static variable or static methods.

Syntax:
Static <Class name> (no parameter)
Static constructor does not take parameter, does not require access modifier

Example:
class Mathematic
{
public static int num1, num2;// we can also write num1, num2 like this
        static Mathematic()
        {
            num1 = 20;
            num2 = 90;
            Console.WriteLine("static variable are initiallized");
        }
        public static int sum()
        {
            return num2 + num1;
        }
      
}
class Program
{
static void Main(string[] args)
       {
             int sum= Mathematic.sum();
            Console.WriteLine("sum is: "+sum);
            Console.ReadLine();
       }
      

}
Output:
Static variable are initialized
Sum is 110

Destructor

Destructor is a special type method which has the same name like Constructor but it starts from ~ character.
The purpose of destructor is to de-allocate the memory of objects automatically that are no longer required.
We have no control on destructor when it is going to execute.
It does not require access modifiers.

Example:
class Office
{
public string name, fatherName, Age;
public Office(string _name,string _fatherName, string _age)
       {
            name = _name;
            fatherName = _fatherName;
            Age = _age;
     Console.WriteLine("constructor has initialized variables");
}
       ~Office()
       {
            Console.WriteLine("this is destructor");
       }
      
}
class Program
{
static void Main(string[] args)
{

              Office objMath = new Office("wasim","yameen","23");
          
              Console.ReadLine();
}
      
}

Output:
Constructor has initialized variables

No comments:

Post a Comment