The "Hello, World" program is traditionally used to introduce a programming language. Here it is in C#:
using System;
class Hello { static void Main() { Console.WriteLine("Hello, World"); } }
The "Hello, World" program starts with a using directive that references the System namespace. Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other namespaces—for example, the System namespace contains a number of types, such as the Console class referenced in the program, and many other namespaces, such as IO and Collections. A using directive that references a given namespace enables unqualified use of the types that are members of that namespace. Because of the using directive, the program can use Console.WriteLine as shorthand for System.Console.WriteLine.
The Hello class declared by the "Hello, World" program has a single member, the method named Main. The Main method is declared with the static modifier. While instance methods can reference a particular enclosing object instance using the keyword this, static methods operate without reference to a particular object. By convention, a static method named Main serves as the entry point of a C# program.
The output of the program is produced by the WriteLine method of the Console class in the System namespace. This class is provided by the standard class libraries, which, by default, are automatically referenced by the compiler.
Namespaces have the following properties:
- They organize large code projects.
- They're delimited by using the . operator.
- The using directive obviates the requirement to specify the name of the namespace for every class.
- The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.
A type defines the structure and behavior of any data in C#. The declaration of a type may include its members, base type, interfaces it implements, and operations permitted for that type. A variable is a label that refers to an instance of a specific type.
There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data. Variables of reference types store references to their data, the latter being known as objects. With reference types, it's possible for two variables to reference the same object and possible for operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it isn't possible for operations on one to affect the other (except for ref and out parameter variables).
An identifier is a variable name. An identifier is a sequence of unicode characters without any whitespace. An identifier may be a C# reserved word, if it's prefixed by @. Using a reserved word as an identifier can be useful when interacting with other languages.
C#'s value types are further divided into:
- simple types
- enum types
- struct types
- nullable value types
- tuple value types
C#'s reference types are further divided into:
- class types
- interface types
- array types
- delegate types
Classes are declared by using the class keyword followed by a unique identifier, as shown in the following example:
//[access modifier] - [class] - [identifier]
public class Customer {
// Fields, properties, methods and events go here...
}
An optional access modifier precedes the class keyword. Because public is used in this case, anyone can create instances of this class. The name of the class follows the class keyword. The name of the class must be a valid C# identifier name. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.
Although they're sometimes used interchangeably, a class and an object are different things. A class defines a type of object, but it isn't an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class.
Objects can be created by using the new keyword followed by the name of the class, like this:
Customer object1 = new Customer();
When an instance of a class is created, a reference to the object is passed back to the programmer. In the previous example, object1 is a reference to an object that is based on Customer. This reference refers to the new object but doesn't contain the object data itself. In fact, you can create an object reference without creating an object at all:
Customer object2;
We don't recommend creating object references that don't refer to an object because trying to access an object through such a reference fails at run time. A reference can be made to refer to an object, either by creating a new object, or by assigning it an existing object, such as this:
Customer object3 = new Customer();
Customer object4 = object3;
This code creates two object references that both refer to the same object. Therefore, any changes to the object made through object3 are reflected in subsequent uses of <object4. Because objects that are based on classes are referred to by reference, classes are known as reference types.
The members of a class are either static members or instance members. Static members belong to classes, and instance members belong to objects (instances of classes).
The following list provides an overview of the kinds of members a class can contain.
- Constants: Constant values associated with the class
- Fields: Variables that are associated with the class
- Methods: Actions that can be performed by the class
- Properties: Actions associated with reading and writing named properties of the class
- Indexers: Actions associated with indexing instances of the class like an array
- Events: Notifications that can be generated by the class
- Operators: Conversions and expression operators supported by the class
- Constructors: Actions required to initialize instances of the class or the class itself
- Finalizers: Actions done before instances of the class are permanently discarded
- Types: Nested types declared by the class
Each member of a class has an associated accessibility, which controls the regions of program text that can access the member. There are six possible forms of accessibility. The access modifiers are summarized below.
- public: Access isn't limited.
- private: Access is limited to this class.
- protected: Access is limited to this class or classes derived from this class.
- internal: Access is limited to the current assembly (.exe or .dll).
- protected internal: Access is limited to this class, classes derived from this class, or classes within the same assembly.
- private protected: Access is limited to this class or classes derived from this type within the same assembly.
All the documentation in this page is taken from Microsoft Learn