Method
can be declared with various access specifier, here are the name of
access specifier which
public
class
Student
{
public
double
CalculateFees()
{
double
d
= 10.00;
return
d;
}
}
//
Mehod Calling
Student
obj
= new
Student();
double
fees
= obj.CalculateFees();
{
private
double
CalculateFees()
{
double
d
= 10.00;
return
d;
}
}
//
Mehod Calling
Student
obj
= new
Student();
double
fees
= obj.CalculateFees();//error
inaccessable due to protection level
public
class
Student
{
public
void
CalculateFees()
{
Method2();
}
protected
virtual
void
Method2()
{
Console.WriteLine("I
am at student level");
}
}
public
class
Child
:
Student
{
protected
override
void
Method2()
{
Console.WriteLine("I
am at child level");
}
}
//
Mehod Calling
var
b
= new
Student();
b.CalculateFees();
//
returns "I am at student level"
var
d
= new
Child();
d.CalculateFees();
//
returns "I am at child level"
{
}
Example
2
class
Student
{
sealed
override
public
void
GetFees()
{
Console.WriteLine("In
class B");
}
}
class
Child
:
Student
{
override
public
void
GetFees()
{
//Error
1 'Student.GetFees()': no suitable method found
//Error
2 'Child.GetFees()': cannot override inherited member
'Student.GetFees()
}
}
simultaneously the application, program can run other statement also while async methods running.
{
await
Task.Run(()
=>
{
for
(int
i
= 0; i < 100; i++)
{
Console.WriteLine("
asyncMethod ");
}
});
}
Parameter Array : If method is called a parameter type of array, the whole
copy of array copied into
the
original method. The array can be any type integer, string, Boolean.
A Method can also return array,
Example 1
//Method
calling
int[]
roll = new int[]
{ 1, 2, 3 };
PassArayMethod(roll);
Response.Write(roll[0]);
//out
put:45
//Method
declaration
public
void
PassArayMethod(int[]
data)
{
data[0]
= 45;
}
static
int[]
ArrayReturnMethod(int
l)
{
int[]
arr = new
int[l];
for
(int
a
= 0; a < l; a++)
{
arr[a]
= 5 * a;
}
return
arr;
}
//Method
calling
int[]
roll = ArrayReturnMethod(6);
Response.Write(roll[0]);
Response.Write(roll[1]);
Response.Write(roll[2]);
Response.Write(roll[3]);
//out
put:0
//out
put:5
//out
put:10
//out
put:15
Example 1
{
public
string
GetFees()
{
return
"Method
with Zero Argumant";//code
ignored
}
public
string
GetFees(int
roll_number)
{
return
"Method
with Single Argumant";//code
ignored
}
public
string
GetFees(DateTime
dateOfBirth,
string
Name)
{
return
"Method
with Double Argumant";//code
ignored
}
}
//Method calling
Student
obj
= new
Student();
Response.Write(obj.GetFees()+
"
");
");
Response.Write(obj.GetFees(123)+
"
");
");
Response.Write(obj.GetFees(DateTime.Today,"John")+
"
");
");
//out
put:Method with Zero Argumant
//out
put:Method with Single Argumant
//out
put:Method with Double Argumant
Method
declared under an interface is by default public. We cannot declare
at them private, protected
No comments:
Post a Comment