Search This Blog

Friday, 20 July 2012

Access Modifiers



public - the member can be reached from anywhere. This is the least restrictive visibility. Enums and interfaces are, by default, publicly visible.

protected - members can only be reached from within the same class, or from a class which inherits from this class.

internal - members can be reached from within the same project only.

protected internal - the same as internal, except that also classes which inherits from this class can reach it members, even from another project.

private - can only be reached by members from the same class. This is the most restrictive visibility. Classes and structs are by default set to private visibility.


Static members

Example:
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;
    }
}
 
 
PREVIOUS CHAPTER

NEXT CHAPTER 

No comments:

Post a Comment