Saturday, March 31, 2012

C# Class and Method Keywords

So I am new to a lot of these concepts and thought I should write down a few things about classes. The basics of a class, it is a template from which object instances can be created. It represents a noun or thing-ness from which other related things can be derived. A class has variables and methods. A variable describes the class, methods describe what a class will do. Classes are used to create instances of objects to be used in your program.

Access Modifiers
public, private, internal, protected

Classes and methods both have keyword / modifiers that define the level of access to the class or method. When a class is declared as public, access is not restricted to any member, unless the member has a more limited access modifier set on it. You can call any public class members from any other class (whether they inherit from the class or not). You cannot call a private class member in a public class from another calls. These methods are only accessible from within the class itself.

A private class prevents its members from being accessed by any other class. This essentially prevents inheritance. A method cannot be declared as public from a private class.A private method in a public class, what would that do?

Private also applies to variables and methods. The scope of the security changes with the scope of the modifier. A private method cannot be accessed by other methods outside the context of the class. Same with a private variable. Declaring these as public ensures other classes can access the method or variable.

Protected is like lazy private. Access is limited to the containing class, or subclasses that inherit from the containing class.That sound shard to understand. What it means is that a class with a protected method / member does not grant other classes access to that method, unless those classes are derived from it. If the whole class is protected no members can be public, but individual members can be private or protected.

Internal limits access of the class members to other classes in the same assembly (think dll). I usually think of this as classes in the same namespace, but I am not betting money that is scientifically accurate.

Protected internal makes no sense to me. It is limited to the current assembly OR types derived from the containing class. Like lazy public, where the only restriction is on classes outside the assembly that are not derived from the current class. Not sure when you would need that.

Static ensures that a variable or method is consistent across all  instances of an object instantiated from a specific class. The value for a static variable can be changed, but it will be changed for all instances of an object. By not declaring something static you are making it an instance method or instance variable. This changes the scope from the class to the object, meaning changing the variable or overriding the method in one object has no effect on other objects. Instance methods and instance variables are not accessible outside the scope of an object. Static methods and static variables are accessible directly from the class and do not require instantiation of an object. 

notes from MSDN
The direct base class of a class type must be at least as accessible as the class type itself.
The return type and parameter types of a method must be at least as accessible as the method itself.
The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.

What the heck concepts:
assembly, containing class, return type

REFERENCES
egghead
object composition

No comments:

Post a Comment