You are on page 1of 6

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be subclassed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesnt support multiple inheritance, interfaces are used to implement multiple inheritance.

Both Together
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface. When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes. There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison: Feature Multiple inheritance Default implementation Interface A class may inherit several interfaces. An interface cannot provide any code, just the signature. Abstract class A class may inherit only one abstract class. An abstract class can provide complete, default code and/or just the details that have to be overridden. An abstract class can contain access modifiers for the subs, functions, properties An abstract class defines

Access Modfiers

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public Interfaces are used to

Core VS Peripheral

Feature

Interface define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

Abstract class the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces. Requires more time to find the actual method in the corresponding classes. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. No fields can be defined in interfaces

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use. Fast

Speed

Adding functionality (Versioning)

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. An abstract class can have fields and constrants defined

Fields and Constants

Difference between string and string builder


Both String and StringBuilder are classes used to handle the strings. The most common operation with a string is concatenation. This activity has to be performed very efficiently. When we use the "String" object to concatenate two strings, the first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted. This process is a little long. Hence we say "Strings are immutable". When we make use of the "StringBuilder" object, the Append method is used. This means, an insertion is done on the existing string. Operation on StringBuilder object is faster than String operations, as the copy is done to the same location. Usage of StringBuilder is more efficient in case large amounts of string manipulations have to be performed.

Difference Between ToString() vs Convert.ToString()


Both perform the same function. But the main difference is: ToString() raise exception when the object is null

So in the case of object.ToString(), if object is null, it raise NullReferenceException. Convert.ToString() return string.Empty in case of null object.

Some notable distinguishing features of C# are:

1. C# is a simple, modern, object oriented language derived from C++ and Java. 2. It aims to combine the high productivity of Visual Basic and the raw power of C+
+.

3. We may develop Console application, Windows application, and Web application


using C#.

4. 5. 6. 7. 8. 9.

It supports garbage collection, automatic memory management and a lot. Pointers are missing in C#.

Unsafe operations such as direct memory manipulation are not allowed. In C# there ranges of is no the usage primitive of types "::" like or "->" operators. Floats etc.

Varying

Integer,

10.

Integer values of 0 and 1 are no longer accepted as Boolean values. Boolean values are pure true or false values in C# so no more errors of "="operator and "=="operator. "==" is used for comparison operation and "=" is used for assignment operation. In C# we cannot perform unsafe casts like convert double to a Boolean.

11. Value types (primitive types) are initialized to zeros and reference types (objects
and classes are initialized to null by the compiler automatically. are bound checked.

12.

Arrays

are

zero

base

indexed

and

13. Overflow of types can be checked. 14. Allows restricted use of native pointers. 15. C# allows the users to use pointers as unsafe code blocks to manipulate your old
code.

CONCLUSION C# is a modern, type safe programming language, object oriented language that enables programmers to quickly and easily build solutions for the Microsoft .NET platform.

Differences between a class and a structure in C#: Class


In C# language, a class is defined by using the class keyword. For example, public class StudentReport{.}

Structure

In C# language, a structure is defined by using the struct keyword. For example, public struct StudentReport{.} A class is a reference type and its object is A structure is a value type and its object created on the heap. is created on the stack. A structure does not support inheritance A class can be inherited from a class or and cannot be inherited from any class struct. or struct. A class is more efficient when used in A structure is more efficient when used collections. in arrays. A class can have a default constructor A structure cannot have any without any parameter, which is provided by parameterless constructor or the default the common language runtime (or CLR). It constructor that is provided by the CLR. also supports destructors. It also cannot have destructors. An instance field or a member variable can An instance field or a member variable be initialized in a class. cannot be initialized in a struct. A class cannot be created without using the new keyword. For A struct can be created without using example, StudentReport StudRepo = new the new keyword. For StudentReport(stud_enroll, stud_name, example, StudentReport StudRepo; stud_report); All members of a class are private by All members of a structure are public by default. default.

Differences between a value types and reference types in C#: Value types
A variable that is of type value directly contains a value. Assigning a variable of type value to another variable of type value COPIES that value

Reference types
A variable of type reference, points to a place in memory where the actual object is contained. Assigning a variable of type reference to another variable of type reference copies that reference (it tells the new object where the place in memory is), but does not make a copy of the object. Reference types are stored on heap. Reference types can contain the value null. An object is created in memory and then handled through a separate reference.

Value types are stored on the stack. Value types cannot contain the value null. When a value type is created, a single space in memory is allocated to store the value.

E.g.: int, float, bool, char, structures, enumerations

e.g.: classes, objects, strings, arrays

Differences between array and ArrayList in C#: ARRAY ARRAYLIST


ArrayList a_list=new ArrayList(); ArrayList is in the System.Collections namespace. ArrayList can increase and decrease size dynamically ArrayList can hold item of different types ArrayList always has exactly one dimension The lower bound is always zero.

1. 2. 3. 4. 5. 6.

Char[] vowel=new Char[]; Array is in the System namespace The capacity of an Array is fixed An Array is a collection of similar items An Array can have multiple dimensions The lower bound of an Array can be set.

Polymorphism and its types:


Polymorphism provides following features: It allows you to invoke methods of derived class through base class reference during runtime. It has the ability for classes to provide different implementations of methods that are called through the same name.

Polymorphism is of two types: 1. Compile time polymorphism/Overloading 2. Runtime polymorphism/Overriding Compile Time Polymorphism Compile time polymorphism is method and operators overloading. It is also called early binding. In method overloading method performs the different task at the different input parameters. Runtime Time Polymorphism Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

When overriding a method, you change the behaviour of the method for the derived class. Overloading a method simply involves having another method with the same prototype.

You might also like