You are on page 1of 6

Interface

 An interface contains only the signatures of methods, properties, events or indexers.


 An interface can't contain constants, fields, operators, instance constructors, destructors, or types.
 A class or struct that implements the interface must implement all the members of the interface that
are specified in the interface definition irrespective of whether it is abstract or not.
 An interface can inherit from one or more base interfaces.
 When a base type list contains a base class and interfaces, the base class must come first in the list.
 A class that implements an interface can explicitly implement members of that interface. An explicitly
implemented member cannot be accessed through a class instance, but only through an instance of
the interface.
 Explicitly implemented methods and properties are private and inaccessible even to the
implementing class. They do not take any access modifiers either.
 It is possible to have partial implicit and partial explicit implementations of an interface.
 By using interfaces, you can, for example, include behavior from multiple sources in a class. That
capability is important in C# because the language doesn't support multiple inheritance of class. In
addition, you must use an interface if you want to simulate inheritance for structs, because they can't
actually inherit from another struct or class.
 Interface members are automatically public, and they can't include any access modifiers. Members
also can't be static.
 To implement an interface member, the corresponding member of the implementing class must be
public, non-static, and have the same name and signature as the interface member. Here signature
also includes return type.
 If a base class implements an interface, any class that's derived from the base class inherits that
implementation.
 Properties and indexers of a class can define extra accessors for a property or indexer that's defined in
an interface. For example, an interface might declare a property that has a get accessor. The class that
implements the interface can declare the same property with both a get and set accessor. However, if
the property or indexer uses explicit implementation, the accessors must match.

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. 

 The following list provides the main features of a static class:


o Contains only static members.
o Cannot be instantiated.
o Is sealed.
o Cannot contain Instance Constructors.
 Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class
except Object.
 Static methods can be overloaded but not overridden, because they belong to the class, and not to
any instance of the class.
 A const field is essentially static in its behavior. It belongs to the type, not to instances of the type.
Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is
used for static fields. No object instance is required.
 Elements defined in a namespace cannot be explicitly declared as private, protected, or protected
internal but nested types can be marked with any of these keywords.
 Top-level types, which are not nested in other types, can only have internal or public accessibility. The
default accessibility for these types is internal.
 When an object of a child class is created, all the data members of the child class and the parent class
are allocated memory irrespective of their access modifiers i.e. public or private or protected.
 Const is compile-time constant whereas readonly is run-time constant. Hence readonly can be
assigned even in constructor whereas const has to be assigned at the time of declaration itself.
 Protected internal is internal within the assembly and protected outside the assembly. To limit
accessibility to only derived classes in the same assembly, declare the class itself internal, and declare
its members as protected.

Restrictions on Using Accessibility Levels


Context Remarks

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

CA a = Static Static Static Dynamic Dynamic Dynamic Dynamic Dynamic Dynamic


new CA CA CA CA CB CB CA CA CB
CB();
a.Foo();
CB b = Static Dynami Dynamic Static Dynamic Dynamic Dynamic Dynamic Dynamic
new CB c CC CB CB CC CC CB CB
CC(); CB
b.Foo();
CA a = Static Static Static Dynamic Dynamic Dynamic Dynamic Dynamic Dynamic
new CA CA CA CA CB CC CA CA CB
CC();
a.Foo();

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")]

Interface vs Abstract class


Feature Interface Abstract class
Multiple A class may inherit several interfaces. A class may inherit only one abstract class.
inheritance
Default An interface cannot provide any code, just the An abstract class can provide complete,
implementation signature. default code and/or just the details that
have to be overridden.
Access An interface cannot have access modifiers for An abstract class can contain access
Modfiers the subs, functions, properties etc everything modifiers for the subs, functions, properties
is assumed as public
Core VS Interfaces are used to define the peripheral An abstract class defines the core identity of
Peripheral abilities of a class. In other words both Human a class and there it is used for objects of the
and Vehicle can inherit from a IMovable same type.
interface.
Homogeneity If various implementations only share method If various implementations are of the same
signatures then it is better to use Interfaces. kind and use common behaviour or status
then abstract class is better to use.
Speed Requires more time to find the actual method Fast
in the corresponding classes.
Adding If we add a new method to an Interface then If we add a new method to an abstract class
functionality we have to track down all the then we have the option of providing
(Versioning) implementations of the interface and define default implementation and therefore all
implementation for the new method. the existing code might work properly.
Fields and No fields can be defined in interfaces An abstract class can have fields and
Constants constrants defined

You might also like