Encapsulation is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits.
For more information about classes, see Classes (C# Programming Guide) and Objects (C# Programming Guide). For more information about how to design classes, see Type Design Guidelines.
Members
All methods, fields, constants, properties, and events must be declared within a type; these are called the members of the class or struct. In C#, there are no global variables or methods as there are in some other languages. Even a program's entry point, the Main method, must be declared within a class or struct. The following list includes all the various kinds of members that may be declared in a class or struct.
Accessibility
Some methods and properties are meant to be called or accessed from code outside your class or struct, known as client code. Other methods and properties might be only for use in the class or struct itself. It is important to limit the accessibility of your code so that only the intended client code can reach it. You specify how accessible your types and their members are to client code by using the access modifierspublic , protected , internal, protected internal, and private . The default accessibility is private. For more information, see Access Modifiers (C# Programming Guide).
Inheritance
Classes (but not structs) support the concept of inheritance. A class that derives from another class (the base class) automatically contains all the public, protected, and internal members of the base class except its constructors and destructors. For more information, see Inheritance (C# Programming Guide) and Polymorphism (C# Programming Guide).
Classes may be declared as abstract, which means that one or more of their methods have no implementation. Although abstract classes cannot be instantiated directly, they can serve as base classes for other classes that provide the missing implementation. Classes can also be declared as sealed to prevent other classes from inheriting from them. For more information, see Abstract and Sealed Classes and Class Members (C# Programming Guide).
Interfaces
Classes and structs can inherit multiple interfaces. To inherit from an interface means that the type implements all the methods defined in the interface. For more information, see Interfaces (C# Programming Guide).
Generic Types
Classes and structs can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example The List<(Of <(T>)>) class in the System.Collections.Generic namespace is defined with one type parameter. Client code creates an instance of a List<string> or List<int> to specify the type that the list will hold. For more information, see Generics (C# Programming Guide).
Static Types
Classes (but not structs) can be declared as static. A static class can contain only static members and cannot be instantiated with the new keyword. One copy of the class is loaded into memory when the program loads, and its members are accessed through the class name. Both classes and structs can contain static members. For more information, see Static Classes and Static Class Members (C# Programming Guide).
Nested Types
A class or struct can be nested within another class or struct. For more information, see Nested Types.
Partial Types
You can define part of a class, struct or method in one code file and another part in a separate code file. For more information, see Partial Classes and Methods.
Object Initializers
Anonymous Types
In situations where it is not convenient or necessary to create a named class, for example when you are populating a list with data structures that you do not have to persist or pass to another method, you use anonymous types. For more information, see Anonymous Types (C# Programming Guide).
Extension Methods
You can "extend" a class without creating a derived class by creating a separate type whose methods can be called as if they belonged to the original type. For more information, see Extension Methods (C# Programming Guide).
Implicitly Typed Local Variables