Saturday, June 1, 2013

// // Leave a Comment

static vs instance members in C Sharp

Static Versus Instance Members in C#


  • By default, members are per instance
  • Each instance gets its own fields
  • Methods apply to a specific instance
  • Static members are per type
  • Static methods can’t access instance data
  • No this variable in static methods


  Static methods

     public class Area
  {
    private int side;

    public Rectangle(int side)
    {
        this.side= side;
    }

    public void Area()
    {
        Console.WriteLine("Area output: " + Area.CalculateArea(this.side));
    }

    public static int CalculateArea(int side)
    {
        return side* side;
     }
  }

Sample


Static menbers versus insatance members in C#

0 comments:

Post a Comment