Wednesday, 19 August 2015

What is difference between instance Methods and Static Methods in C#.Net

The Main Difference between them are:
  • A Method declare using Static modifier was a static method. Rest of all were Instance only.
  • While defining Static Methods we can't consume any instance members of the class directly. They can only be consumed by creating the object of the class.
Note :
           Non-Static members of the class can't be consumed from static blocks directly. They should be referred only by using object of the class.

Sample Example for Instance Methods and Static Methods :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
    class staticMethods
        int x = 100;
        static int y = 200;
        static void Add()
            staticMethods sm = new staticMethods();
            Console.WriteLine(sm.x + y);
        static void Main()

Output :

No comments:

Post a Comment