Scope modifiers: Difference between revisions

Added C#
(Added C#)
Line 293:
 
// ...</lang>
 
=={{header|C sharp}}==
<lang csharp>public //visible to anything.
protected //visible to current class and to derived classes.
internal //visible to anything inside the same assembly (.dll/.exe).
protected internal //visible to anything inside the same assembly and also to derived classes outside the assembly.
private //visible only to the current class.
 
//Modifier | Class | Assembly | Subclass | World
//--------------------------------------------------------
//public | Y | Y | Y | Y
//protected internal | Y | Y | Y | N
//protected | Y | N | Y | N
//internal | Y | Y | N | N
//private | Y | N | N | N</lang>
If no modifier is specified, it defaults to the most restrictive one.<br/>
In case of top-level classes/structs/interfaces/enums this means internal, otherwise it means private.
 
Special case: explicit interface implementation.<br/>
When a class explicitly implements an interface method, it is 'hidden' and that method can only be accessed through the interface:
<lang csharp>public interface IPrinter
{
void Print();
}
 
public class IntPrinter : IPrinter
{
void IPrinter.Print() { // explicit implementation
Console.WriteLine(123);
}
 
public static void Main() {
//====Error====
IntPrinter p = new IntPrinter();
p.Print();
 
//====Valid====
IPrinter p = new IntPrinter();
p.Print();
}
}
</lang>
Variables, parameters, methods, etc use lexical scoping.<br/>
Visibility is determined by the enclosing braces { }<br/>
 
=={{header|Common Lisp}}==
196

edits