Method /behaviour:
Method is a member of a class which will have some behaviour or
functionality.
Q) When we will go for method?
A) Whenever we want to implement some functionality we will go for method.
Eg: - we want to calculate total sal of the employee then we can go for a
method called “calsal ()”.
Syntax for method:
<Access modifier> <return type> (<type><arg1,<type><arg2>)
{
//functionality
}
A method which is not returning any value, that method return type
should be void, which is called as void method.
Ex: public void display ()
{
Console.WriteLine (“hi”);
}
2. Non void method:
If a method is returning some value, then particular method is a non void
method.
Non void method return type should be the type of value which is
returning by that method.
If the method is returning numerical value then method return type should
be numerical data type
If the method is returning floating value, method return type should be
any one of the floating data type and so on...
Ex:
Public int Add (int a, int b)
{
Int c=a+b;
Return c;
}
Add (10, 5);
Mainly methods are classified into two types
1. Instance/non static method.
2. Static method.
1. Instance method:
While defining a method, if we dint use static keyword, that method is
called as instance method (or) non static method.
Instance method, we have to access with the help of object (or) instance.
Ex:
Public void show ()
{
}
2. Static method:
While defining a method, if we have used static keyword that method is
called as static method.
Static method, we have to access with the class name.
Ex:
Public static void show ()
{
}
Memory allocation:
No memory will be allocated for class.
No memory will be allocated for method.
Memory will be allocated for variables.
When the object is created, memory will be allocated for instance variable.
When the object is destroyed, memory will be de allocated for instance
variable.
In dot net, object can be destroyed in two ways:
a) By garbage collector.
b) By programmer.