Everything is a class. Nothing can be created without class. You cannot create some non-class procedures and functions, you can create any functionality only as class methods.
Like in Delphi only single inheritance is allowed in C#. In other words, a class can inherit implementation from one base class only. However, a class can implement more than one interface. You can also declare generic classes that have type parameters.
- A class can contain declarations of the following members:
-
Constructors
Destructors
Constants
Fields
Methods
Properties
Indexers
Operators
Events
Delegates
Classes
Interfaces
Structs
Class members can be marked as public, private, protected, internal, or protected internal.
Private members are accessible only within the body of the class or the struct in which they are declared.
A protected member is accessible within its class and by derived classes.
Internal types or members are accessible only within files in the same assembly.
Public access is the most permissive access level. There are no restrictions on accessing public members.
All classes are inherited from Object, so if the ancestor is not specified, class is derived from Object.
A field can optionally be declared static. This makes the field available to callers at any time, even if no instance of the class exists. Also field can be declared readonly. A read-only field can only be assigned a value during initialization or in a constructor. A static readonly field is very similar to a constant.
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.
A method is a code block containing a series of statements.
A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature.
Events are a way of providing notifications about occurrences, such as button clicks or the successful completion of a method, to other objects. Events are defined and triggered using delegates.
Operators are terms or symbols such as +, *, <, and so on that perform operations on operands. Operators can be redefined to perform operations on custom data types.
Indexers allow an object to be indexed in a manner similar to arrays.
Constructors are methods that are called when the object is first created. They are often used to initialize the object's data.
Destructors are methods that are called by the runtime execution engine when the object is about to be removed from memory. They are generally used to make sure that any resources which need to be released are handled appropriately.
Nested Types (classes, interfaces and structs) are types declared within a class. Nested Types are often used to describe objects used only by the types containing them.
Class are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This process is known as boxing, and the reverse process is known as unboxing. Boxing and unboxing allow any type to be treated as an object.
Within its methods, object can be accessed by the predefined this variable:
class Test
{
private int a;
Test(int val)
{
this.a = val;
}
}
|
|
All functionality can be created within class methods only, you cannot create external functions and procedures. Very useful with has no analogue in C#. |
|
|
|
You can redefine any operators in your own class to simplify the application code. |
|
|
|
|
|
|
|
|
|