Monday, 5 March 2018

009 C# tutorial Method Part 1

C # tutorial 009 Method
Method : A method is a part of a program which do some work. From a method , we can calculate value, calculate age, access data, manipulate data and many more. We place method to the class the as method to some work for the class, thus method become the member of a class which has some specific work to do . Method are two type, it can return value or do not return value. Below is the template of a method. 
  • Access specifier : Private, public, protected etc.
  • Return type : A method can return a value of type int, string, byte or it may return no value that is void.  
  • Method name : We should give a name to a method to identify the method and call the method with   this name.Method names are unique and case sensitive
  • Parameter list : We can pass a list of parameter to the method for doing its work and action.Method parameter list games with enclosed parenthesis.
  • Body : Body of a method is written as per the developer requirement.
Here is an example of a method

Example 1
class A
{
       public void add()//method declaration
        {
        //Body
         }
}
 
Calling Method : A method is active when it is called or we can say a method do its work when it is called. Here is a example method calling. 


Example 1
class A
{
        public string GetValue()
        {
         string reVal="Hello World";
         //body statement ignore intentionally
         return reVal;
        }
}


//method call 
A obj = new A();
string s=obj.GetValue();


Method Variables :A list of variable declared within the method called method variable. Statement executed within a method use this variable. Here is the example of method variables. 


Example 1
class A
{
            public string GetValue()
             {
             int age;
             string Name;
             byte[] profile_img;
             double b;
             string reVal = "Hello World";
            //body statement ignore intentionally
             return reVal;
           }
}




Method Parameter : Parameters are list of value or reference pass to the method. Parameters are those variables  ,whose value used for doing its action using those values. Parameter type and number should be match with the declaration and from the calling program. Calling program should send same number of arguments with same type to the method. Two type of parameter passing is possible, pass by value, pass by reference. 


When Pass By Value is called, copy of data from the calling location copy to the original method.  Pass by value do not have any change in the actual variable if the variable change in the method
 
Pass by Reference is a process where variable does not contain value, it is the memory address of the value. The original memory address of the value is passed to the method Any change in the method will reflect to the original object, as the original method at this as past and it was changed. The change will reflect as the method has changed the original object. . Here are the example of pass by Value and pass by reference examples.


Example 1
public string GetValue(int age, string Name, byte[] profile_img, double b)
{
        string reVal = "Hello World";
       //body statement ignore intentionally
        return reVal;
}
 Example 2
 
public string GetValue(ref int age, ref string Name, ref byte[] profile_img, ref double b)
{
           string reVal = "Hello World";
             //body statement ignore intentionally
           return reVal;
}


Output Parameter : C# allow a parameter can be used for output. Out keyword is used for this purpose. Output parameter always return reference. When more than one return , generally we use the output parameter.Here is the example of output parameter.


Example 1
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
    {
         int age;
        string Name;
        double fees;


         if (GetFees(10, "John", out fees))
        {
         Console.WriteLine("Fees is :" + fees.ToString());
       }
}
static bool GetFees(int age, string Name, out double fees)
   {
   fees = 200;
   return true;
   }
}




Optional Parameter : optional at the keyword used to tell compiler this parameter is not mandatory. That means while calling the method, user may or may not give the parameter value. C# allow optional parameter as a last argument. Below is the example of optional parameter. 


Example 1 
public partial class _Default : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
         GetFees(10, "John");
         GetFees(10, "John",15);
       }
      public string GetFees(int age, string Name,int RollNumber=0)
      {
      //Code Ignores
      return "Hello";
     }
}


Recursive : Recursion is a process of programming which call itself. When a method call itself repeatedly, it is called recursive process. Here is the example of recursion.



Example 1
             private static long Factorial(int n)
            {
            if (n == 0)
            {
              return 1;
             }
            return n * Factorial(n - 1);


Calling:
   long l = Factorial(5);              //output :120 






No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...