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  



No comments:

Post a Comment

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

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