RSS
Bhala Ganpatye

C# Fundamentals - Commonly compared concepts (differences)

Sun, Jan 22, 2012

Bhala Ganpatye

I have been asked many time questions like

  • When should I use an abstract class and when I should use an interface?
  • What is difference between delegate and event?

Although people know few points of differences, it’s worth to consolidate as many as differences as we can. So I am trying to provide most of the points that can not only compares various concepts, but also helps developers understand concepts better way.

Override vs. Overload

Overriding

Overloading

The argument list must exactly match as that of the overridden method

Overloaded methods must change the argument list (type and/or numbers of arguments)

The return type must exactly match as that of the overridden method

Overloaded methods can change the return type

The access level can be same or less restrictive than that of the overridden method. (more restrictive not allowed)

Overloaded methods can change the access modifier. Methods can have difference access modifiers

The overriding method must not throw new or broader checked exceptions than those declared by the overridden method. But it can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.

Overloaded methods can declare new or broader checked exceptions.

You cannot override a method marked as sealed

You cannot overload a method just by changing only return type

You can override method in derived class only

A method can be overloaded in the same class or in a subclass.

You can override only non-static method

You can overload both static and non-static method

Runtime polymorphism

Compile time polymorphism

Overriding also applies to property and event

Overloading also applies to operator

 

Interface vs. Abstract Class

Feature

Interface

Abstract class

Inheritance in class

A class may inherit several interfaces.

A class may inherit only one abstract class.

Inheritance in structure

An Interface can be inherited by structures.

An abstract class cannot be inherited by structures

Inheritance to self

Interface can inherit only interface(s)

An abstract class may inherit one abstract class and/or multiple interfaces

Default implementation

An interface cannot provide any code, just the signature. Implicit Abstract

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modifiers

An interface cannot have access modifiers for members. everything is assumed as public

An abstract class can contain access modifiers for the methods, properties

Adding functionality

Changing member or adding new member breaks the classes implementing interface. So all subclasses must change implementation

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Allowed Members

properties, methods, events

properties, methods, events, fields, constants, delegates

Constructors

An Interface cannot contain constructors or destructors.

An abstract class can contain constructors or destructors.

 

Contract

Common behavior

 

Value Type vs. Reference Type

Value Type / Struct

Reference Type

Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value.

The assignment of reference type variables, copies a reference to the object but not the object itself.

Value types derive from System.ValueType, which derives from System.Object

Reference types are derived from System.Object

Value types are sealed. you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.

You can derive a new type from another reference type or class

A value type cannot contain the null value. However, the nullable types feature does allow for values types to be assigned to null.

Reference types can be null

Each value type has an implicit default constructor that initializes the default value of that type.

Explicit constructor need to be called to hold data

Value type variables directly contain their values which means that the memory is allocated inline in whatever context the variable is declared.

The memory is allocated on the managed heap. the variable holds only a reference to the location of the object

There is no separate heap allocation or garbage collection overhead for value-type variables.

GC plays important role in memory clean up

 

Delegate vs. Interface

Delegate - Usage 

Interface - Usage

An eventing design pattern is used.

The class using the interface will want to cast that interface to other interface or class types.

It is desirable to encapsulate a static method.

The method being implemented is linked to the type or identity of the class: for example, comparison methods.

The caller has no need to access other properties, methods, or interfaces on the object implementing the method.

There is a group of related methods that may be called.

Easy composition is desired.

The method being implemented is linked to the type or identity of the class: for example, comparison methods.

A class may need more than one implementation of the method.

A class only needs one implementation of the method.

 

Popularity: 11% [?]

, , , , ,

Leave a Reply