Wednesday, September 10, 2014

Static Member in C#

A good explanation and examples are found at http://csharp.net-tutorials.com/classes/static-members/

public class Rectangle
{
    private int width, height;
    public Rectangle(int width, int height)
    {
        this.width = width;
        this.height = height;
    }
    public void OutputArea()
    {
        Console.WriteLine("Area output: " + Rectangle.CalculateArea(this.width, this.height));
    }
    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}


I like the author's example using Rectangle.CalculateArea( this.width, this.height) as an example of using a static member in a non-static class.

No comments: