There are five of them available in C#: public, protected, internal, protected internal and private. The default access level for members of a class is private.
Private access
All members regardless of access level are accessible in the class where they are declared, theenclosing class. This is the only place where a private member can be accessed.
class MyBase { // Unrestricted access public int myPublic; // Defining assembly or derived class protected internal int myProtInt; // Defining assembly internal int myInternal; // Defining or derived class protected int myProtected; // Defining class only private int myPrivate; void Test() { myPublic myProtInt myInternal myProtected myPrivate } }
= = = = =
0; 0; 0; 0; 0;
// // // // //
Protected access
A protected member can also be accessed from within a derived class, but it is inaccessible from other classes.
class Derived : { void Test() { myPublic myProtInt myInternal myProtected myPrivate } } MyBase
= = = = =
0; 0; 0; 0; 0;
// // // // //
Internal access
An internal member can be accessed anywhere within the local assembly, but not from another assembly. In .NET, an assembly is either a program (.exe) or a library (.dll).
// Defining assembly class AnyClass { void Test(MyBase m)
{ m.myPublic m.myProtInt m.myInternal m.myProtected m.myPrivate } } = = = = = 0; 0; 0; 0; 0; // // // // // allowed allowed allowed inaccessible inaccessible
// // // // //
Public access
Public access gives unrestricted access from anywhere that the member can be referenced.
// Other assembly class AnyClass { void Test(MyBase m) { m.myPublic = 0; m.myProtInt = 0; m.myInternal = 0; m.myProtected = 0; m.myPrivate = 0; } }
// // // // //
Inner classes
Classes may contain inner classes, which can be set to either one of the five access levels. The access levels have the same effect on inner classes as they do on other members. If the class is inaccessible, it cannot be instantiated or inherited. By default, inner classes are private, which means that they can only be used within the class where they are defined.
class MyBase { // Inner classes (nested classes) public class MyPublic {} protected internal class MyProtInt {} internal class MyInternal {} protected class MyProtected {} private class MyPrivate {} }
This XML document will describe a book so the first tag is called book. Just as in XHTML the tags are recognized by the angle brackets and all tags must have a closing tag (except for the XML declaration). Unlike HTML, XML tags are not predefined. Any tag names can be used, but they shouldnt contain spaces or start with numbers or punctuations. Either upper or lower case letters can be used, as long as the opening and closing tags have same casing.
<?xml version="1.0" encoding="UTF-8"?> <book></book>
This first element is called the root element and each XML document can have only one of them. Inside the root two child elements are included: title and chapter. These are sister elements to each other, because they have the same parent element. All elements can contain both text and child elements.
<?xml version="1.0" encoding="UTF-8"?> <book> <title>Learning XML</title> <chapter>What is XML</chapter> </book>
XML documents use a very simple and self-describing syntax. If you open up the document created in the previous section in a browser it will be displayed as a tree without any stylistic formatting. It is just data wrapped in tags. To make any use of the file a program has to be written that accesses the data.
<?xml version="1.0" encoding="UTF-8"?> <book> <title>Learning XML</title> <chapter>What is XML</chapter> </book>
Keep in mind that XML has very strict rules regarding its structure. If there is an error in the document, for example if a closing tag has been nested improperly, the browser will report the error and wont display the document.
<a></a><b>Allowed</b> <a><b>Allowed</b></a>
<a><b>Invalid</a></b>
Content types
Elements can have four different content types. Below the title element has simple content, which is also called text content. Book has element content, because it contains only elements and no text. Chapter has mixed content, because it contains both text and elements. Finally, the para tag has empty content. Elements with empty content can also be shortened as in XHTML, by removing the end tag and including a slash before the closing bracket of the start tag.
<?xml version="1.0" encoding="UTF-8"?> <book> <title>Text content</title> <chapter>Mixed content <para></para> <para /> </chapter> </book>
Comments
The comment tag can be useful for leaving notes or for temporarily removing a block of XML data. It has the same notation as the one used in HTML. The tag that may span multiple lines and can appear anywhere in the document.
<!-- This is an XML comment -->