Tuesday, March 19, 2013

Constructors in C#.net


-What is the role of the "new" operator in the creation of an object ?

Ans: When u use the "new" operator for creating the object of the class it will internally perform the following:
   -Reads the Classes
   -Calls the Constructors
   -Allocates the memory required for the object

-What is meant by reading the class ?
Ans: Reading the class in the sense it will recongnize each and every member which was defined under the class.

-What is a Constructor ?
Ans: a constructor is a special method available under every class which is responsible for initializing the variables of the class.

-The Name of the constructor method is always same as the class name.

-A constructor is a non value returning method.

-Every class should contain a constructor in it if it has to execute.

-What happens if a constructor is not defined in a class ?
Ans: as a constructor is mandatory for the execution of a class if it was not defined under the class by the programmer while getting compiled the compiler takes the responsibility to define the constructor under that class, so that the class can execute.

-Can we define a constructor explicitly under the class ?
Ans: we can also define a constructor under the class as following:
[<modifiers>] <Name> ( [<param list>] )
{
<Stmts>;
}
-------------------------------------------------------------------------------------
-Constructors of 2 types:
-Default Constructors
-Parameterized Constructors

-Default or Zero Arguments Constructors does not take any parameters, this can be defined either by a programmer or by the compiler.

-Parameterized Construtors can take parameters, this can be defined only by the programmers.

Note: Compiler defines a constructor under the class only if there is no constructor available under the class.

-If a class contains a parameterized constructor in it while creating the object of that class u need to send the values to the parameters.

-To check this do the following changes in the previous program:

1. Make the constructor as parameterized:
        ConDemo(int x)
        {
            Console.WriteLine("Constructor: " + x);
        }

2. Now under the Main the object creation of the class should be as following:
       ConDemo cd1 = new ConDemo(1);
       ConDemo cd2 = new ConDemo(2);
-------------------------------------------------------------------------------------
-Under a class we can have variables declared with in the class as well as with in the blocks like methods or constructors.

-A variable declared under the class is a Class Variable and accessible thru out the class, where as variables declared under a block are block variables and accessible only with in the block.

-If a class and block variable are declared with the same name, with in the block the preference goes to block variables only, in such cases to refer to the class variables from that blocks use the "this" keyword.

-"this" is used for referring to the members of a class like variables or methods.

-To test this change the constructor code in the previous program as following:
Params(int x , int y)  
        {
                this.x = x;
                      this.y = y;
        }


No comments:

Post a Comment

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...