Wednesday, July 2, 2014

Instance Methods Vs Static Methods

Static methods have no instances. They are called with the type name, not an instance identifier. They are slightly faster than instance methods because of this. Static methods can be public or private.

-A method declared with the help of a "static" modifier is a static method and the rest are instance.

-A static method is invoked with the name of its class. eg: WriteLine is a static method under the class Console which is referred as Console.WriteLine.

-A non-static member of a class cannot be referred from a static block direclty, but still if u want to refer this can be done with the object of the class.

Note: While defining static method never refer to the non-static members with out the object of class


using System;
 
namespace Static_var_and_fun
{
  class number
   {
     // Create static variable
     public static int num;
     //Create static method
     public static void power()
      {
        Console.WriteLine("Power of {0} = {1}", num,                num * num);
        Console.ReadLine();
      }
   }
  class Program
   {
     static void Main(string[] args)
      {
        Console.Write("Enter a number\t");
        number.num = Convert.ToInt32(Console.ReadLine());
        number.power();
      }
   }
}


No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...