Monday, 5 March 2018

009 C# tutorial Method Part 2

Method can be declared with various access specifier, here are the name of access specifier which
 method can be declare. 
  • Public 
  • Private 
  • Protected
Here are the example of method access specifiers. 
 
Example of Public 
 public class Student
  {
          public double CalculateFees()
         {
          double d = 10.00;
           return d;
          }
}
// Mehod Calling
Student obj = new Student();
double fees = obj.CalculateFees();


Example of Private
public class Student
{
          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


 Example of Protected
 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"



Sealed : Method signature also define a method behaviour in an application. Can be sealed,
 who is prevent method from inheritance to child object. Here is the example of method signature 
protect method from being inherit. 



Example 1
sealed class Student
{
}
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()
         }
}



Async Method : A method is a statement to do some work, but while a method is doing his work all 
other work of the application stopped, async method is a kind of method which does not force 
the application to stop the other work. While async method is called, other work also possibles 
simultaneously the application, program can run other statement also while async methods running.
 Here is the example of async method. 



Example 1  
public static async Task asyncMethod()
{
      await Task.Run(() =>
       {
          for (int i = 0; i < 100; i++)
         {
         Console.WriteLine(" asyncMethod ");
         }
       });
}






Now we will discuss some advanced topics about an method and its applications. The fast topics
 we will discuss parameter array. 



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,
 to return a method array type, we need to be configure method return type error type. 



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;
}



Example 2
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



Method Overloading : Overloading easy concept of object oriented programming which call the 
method depending upon the parameter passing. We can declare a number of same name method
 in an object. That means, we can define multiple method with same name, but the parameter 
should be different in each case. When we call the method, the compiler make the appropriate match
 of definition with calling program. When appropriate match definition is found compiler call this
 method. This functionality call method overloading. Here are the example of method overloading. 



 Example 1
class Student
{
            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









Here are some interview related question. 



1)What can you specify the accessibility modifier for method inside the interface?  



Method declared under an interface is by default public. We cannot declare at them private, protected
 or any modifier rather than public. 



2)Can you declare an override method static if the original method is not static? 
No, the overriding method and the original method should have the same signature and access 
specifier. 



3)Can you allow a class to be inherited, but prevent method being overridden? 
If I declare a class public, the class will be access from anywhere in the application. 
Now I am declaring a method with signature field. Now the class will be available for generations
 but the method will not be inherited. 



4)Can we define method overloading in different class? 
Method overloading can be possible only in the same class and structure.
 If we define same name method in define classes, that will not act as a method overloading.
 Every object instance is created with his own method and calling this method with object instance
 have no interaction effect with each other. 



5)Can you prevent private Virtual method?  
Private Virtual method cannot be access. 



6)Static method cannot use static member, the statement is true or false? 
True  



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 






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

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