Searching...

Translate

Jump statements(just for explanation)

ü Jump statements(just for explanation)

ü 1.break

   It is possible to force an immediate exit from a loop, bypassing any code remaining in the body of the loop and the loop’s conditional test, by using the break statement.

   When a break statement is encountered inside a loop, the loop is terminated, and program control resumes at the next statement following the loop. When used inside a set of nested loops, the break statement will break out of only the innermost loop.


ü 2.Continue

   It is possible to force an early iteration of a loop, bypassing the loop’s normal control structure. This is accomplished using continue. The continue statement forces the next iteration of the loop to take place, skipping any code in between. Thus, continue is essentially the complement of break

ü 3.Goto

   The goto is C#’s unconditional jump statement. When encountered, program flow jumps to the location specified by the goto. The goto requires a label for operation.

   A label is a valid C# identifier followed by a colon. The label must be in the same method as the goto that uses it and within scope.


*   2.3 Constructor and Destructor

v Constructor

   A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. The general form of a constructor is shown here:

access class-name(param-list)
{
// constructor code
}

   You will use a constructor to give initial values to the instance variables defined by the class or to perform any other startup procedures required to create a fully formed object.

   Also, usually, access is public because constructors are normally called from outside their class. The param-list can be empty, or it can specify one or more parameters.

Þ   Simple or Default constructor.

   All classes have constructors, whether you define one or not, because C# automatically provides a default constructor that causes all member variables to be initialized to their default values. For most value types, the default value is zero. If param–list is empty than the constructor is called simple constructor.

Þ   Parameterized Constructors

   Most often you will need a constructor that accepts one or more parameters. Therefore constructor which has parameters is called as parameterized constructor.

   Parameters are added to a constructor in the same way they are added to a method: just declare them inside the parentheses after the constructor’s name.

Þ   Copy Constructors

   Sometime we need that our object is initialized as other object. Therefore we need to pass an object in parameters to initialize the newly created object. So when we pass an object in parameter then this constructor is called as copy constructor.








Þ   Example

using System;


    class Room
    {
        int length;
        int width;
     
        public Room()//Default Constructor
        {
            length = 10;
            width = 10;
        }
        public Room(int l, int w)//parameterrized constructor
        {
            length = l;
            width = w;
        }
        public Room(Room v)//copy constructor
        {
            length = v.length;
            width = v.width;
        }
              public int Area()
        {
            return (length * width);
        }
    }
       
    class Test
    {
        static void Main(string[] args)
        {
            Room r1 = new Room();
            Room r2 = new Room(5, 5);
            Room r3 = new Room(r1);
            int Area = r1.Area();
            Console.WriteLine("The area of object r1 is = {0}", Area);
            Area = r2.Area();
            Console.WriteLine("The area for object r2 is = {0}", Area);
            Area = r3.Area();
            Console.WriteLine("The area for object r3 is = {0}", Area);
            Console.ReadLine();
        }
    }








1 comments:

Subscribe us