Professional Documents
Culture Documents
Class
A class can be declared abstract. An abstract class contains abstract methods that have a signature
definition but no implementation. Abstract classes cannot be instantiated. They can only be used
through derived classes that implement the abstract methods. By constrast, a sealed class does not
allow other classes to derive from it.
The program cannot specify exactly when the static class is loaded. However, it is guaranteed to be
loaded and to have its fields initialized and its static constructor called before the class is referenced
for the first time in your program.
A static constructor is only called one time, and a static class remains in memory for the lifetime of
the application domain in which your program resides.
Classes The direct base class of a class type must be at least as accessible as the class type itself.
Interfaces The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.
Delegates The return type and parameter types of a delegate type must be at least as accessible as the delegate type
itself.
Constants The type of a constant must be at least as accessible as the constant itself.
Fields The type of a field must be at least as accessible as the field itself.
Methods The return type and parameter types of a method must be at least as accessible as the method itself.
Properties The type of a property must be at least as accessible as the property itself.
Events The type of an event must be at least as accessible as the event itself.
Indexers The type and parameter types of an indexer must be at least as accessible as the indexer itself.
Operators The return type and parameter types of an operator must be at least as accessible as the operator itself.
Constructor The parameter types of a constructor must be at least as accessible as the constructor itself.
s
Constructor
The compiler adds a default parameter-less constructor if one is not provided explicitly.
If the parameter-less constructor is marked as private then the class cannot be inherited since the
parent class constructor is inaccessible.
If there is no parameter-less constructor in the parent class, then every child class constructor must
explicitly link to some other constructor of the parent class using base keyword.
All the parameter-less constructors in the entire inheritance hierarchy will be called when an object of
derived class is created.
As far as instance constructors are concerned, derived class constructor will be called first but base
class constructor will be executed first. The calling allows for passing any arguments from the derived
class constructor to the base class constructor. The execution allows overriding any code that gets
executed in the base class.
For static constructors, the derived class static constructor will be executed first and then the base
class one. This is because static constructor has nothing to do with inheritance and they get called
purely based on which class gets loaded first. So, a derived class will be loaded first then the base class
gets loaded.
Other constructors in the same class can be called using this keyword.
Using base keyword, explicit calls can be made to any constructor in the base class.
A static constructor does not take access modifiers or have parameters.
A static constructor is called automatically to initialize the class before the first instance is created or
any static members are referenced.
The user has no control on when the static constructor is executed in the program.
The order of execution of destructor is child first and then parent which is reverse order of execution
of constructor.
An abstract class can have all types of members which a concrete class can have including constructor,
destructor, static members, etc.
Virtual Inheritance
If you want your derived member to have the same name as a member in a base class, but you do not
want it to participate in virtual invocation, you can use the new keyword.
Virtual members remain virtual indefinitely, regardless of how many classes have been declared
between the virtual member and the class that originally declared it. If class A declares a virtual
member, and class B derives from A, and class C derives from B, class C inherits the virtual member,
and has the option to override it, regardless of whether class B declared an override for that member.
A derived class can stop virtual inheritance by declaring an override as sealed. This requires putting
the sealed keyword before the override keyword in the class member declaration.
Sealed methods can be replaced by derived classes by using the new keyword.
A derived class that has replaced or overridden a method or property can still access the method or
property on the base class using the base keyword.
In C#, derived classes can contain methods with the same name as base class methods.
o The base class method must be defined virtual.
o If the method in the derived class is not preceded by new or override keywords, the compiler
will issue a warning and the method will behave as if the new keyword were present.
o If the method in the derived class is preceded with the new keyword, the method is defined as
being independent of the method in the base class.
o If the method in the derived class is preceded with the override keyword, objects of the derived
class will call that method instead of the base class method.
o The base class method can be called from within the derived class using the base keyword.
o The override, virtual, and new keywords can also be applied to properties, indexers, and
events.
CA
CB
CC
Partial
It is possible to split the definition of a class or a struct, an interface or a method over two or more
source files. Each source file contains a section of the type or method definition, and all parts are
combined when the application is compiled.
The partial keyword indicates that other parts of the class, struct, or interface can be defined in the
namespace. All the parts must use the partial keyword.
All the parts must be available at compile time to form the final type.
All the parts must have the same accessibility, such as public, private, and so on.
If any part is declared abstract, then the whole type is considered abstract.
If any part is declared sealed, then the whole type is considered sealed.
If any part declares a base type, then the whole type inherits that class.
Parts can specify different base interfaces, and the final type implements all the interfaces listed by all
the partial declarations.
Any class, struct, or interface members declared in a partial definition are available to all the other
parts. The final type is the combination of all the parts at compile time.
The following are merged from all the partial-type definitions:
o XML comments
o interfaces
o generic-type parameter attributes
o class attributes
o members
Friend Assemblies
A friend assembly is an assembly that can access another assembly's internal (C#) types and
members. If you identify an assembly as a friend assembly, you no longer have to mark types and
members as public in order for them to be accessed by other assemblies. This is especially convenient
in the following scenarios:
o During unit testing, when test code runs in a separate assembly but requires access to
members in the assembly being tested that are marked as Friend (Visual Basic) or internal (C#).
o When you are developing a class library and additions to the library are contained in separate
assemblies but require access to members in existing assemblies that are marked
as internal (C#).
You can use the InternalsVisibleToAttribute attribute to identify one or more friend assemblies for a
given assembly.
[assembly: InternalsVisibleTo("AssemblyB")]