Visibility and Accessibility Modifiers

Static Modifier

The static modifier keywords can be applied to both type declarations and type members. The static modifier changes the semantics of type construction - in that a static class cannot be instantiated, so you cannot use the new keyword to create a new instance.

using System; public static class ...

 

When you need to access a static member of a class, you use the type name directly. In the example above, I'm using the type name Application to set the Name property.

If you add the static modifer to a class, then all members of that class have to also be marked as static.

// The Application class is marked as static...
public static class Application
{
    // .. So all members have to also be static.
    public static string Name { get; set; }
}

However, you can add a static member to a class without the class requiring the static modifier.

// A non static class
public class Student
{
    // Can still have static members.
    public static string GetSchool()
    {
        return "MIT";   
    }
}

As before, you still access that static member through the type name, e.g. Student.GetSchool().

Page 10 of 18