Core Concepts Revisited: Points to remember
Classes
- A derived class cannot remove the definition of an inherited member, although it can extend the members.
- An implicit conversion exists from a class type to any of its base class types.
For Example:
//Base Class
public class BaseClass
{
public int x, y;
public BaseClass(int x, int y)
{
this.x = x-1;
this.y = y-1;
}
public int Sum()
{ return x + y ; }
}
//Derived Class
public class DerivedClass:BaseClass
{
public int z;
public DerivedClass( int x, int y,int z):base(x,y)
{
this.z = z-1;
}
public int Sum1()
{ return x + y + z; }
}
It is legal to call the Sum function as follows:
BaseClass bc = new BaseClass();
int j= bc.Sum();
OR
BaseClass bc1 = new DerivedClass(4, 5, 6);
int j= bc1.Sum();
- In a chain of derived classes, it is always the base class that gets instantiated first.
- In case the base class constructor expects some parameters, the derived class needs to supply the parameters (as shown in derived class above) or the base class should implement a parameter less parameter.
Fields:
static field:
Marked with static keyword
It does not depend on the instances of class; there is only one copy of a static field ever.
instance filed:
Every instance of a class contains a separate copy of all the instance fields of that class.
read-only fields
These fields are marked with readonly modifier.
Assignment to a readonly field can only occur as part of the field’s declaration or in a constructor in the same class.
Methods:
Signature: The signature of a method does not include the return type.
Parameters:
value parameter:
A value parameter is used for input parameter passing. A value parameter corresponds to a local variable that gets its initial value from the argument that was passed for the parameter. Modifications to a value parameter do not affect the argument that was passed for the parameter.
reference parameter
Marked with ref keyword
A reference parameter is used for both input and output parameter passing.
Example:
public void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
calling the function like this as shown below swaps the two numbers
int i = 1, j = 2;
dp.Swap(ref i, ref j);
Response.Write(string.Format("{0},{1}",i,j));
Note here, we are not returning any values, the input and the output parameters are the same.
output parameter
Marked with the out modifier.
An output parameter is used for output parameter passing. An output parameter is similar to a reference parameter except that the initial value is not important. The following example shows the use of out parameters.
public void Result(int x, int y, out int additionresult, out int substractionresult)
{
additionresult = x + y;
substractionresult = x - y;
}
Call the method as follows:
int sumres,negation;
dp.Result(50, 40, out sumres, out negation);
Response.Write(string.Format("SumResult:{0} SubstractionResult{1}",sumres, negation));
parameter array
Permits a variable number of arguments to be passed to a method.
Declared with the params modifier
Only the last parameter of a method can be a parameter array
The type of a parameter array must be a single-dimensional array type.
Part II will cover some more basics..
Till Then Happy Coding!!
Comments
Post a Comment